Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

EditTitle

Summary: Provide a separate edit field for the page title.
Version: 1.1 (09 Nov 2005)
Prerequisites: PmWiki version 2.1.5
Status: Works for me
Maintainer: Waylan

Question

How can I display an input field in the edit form for editing a page's Title and have the option to require a title be given for every page?

Answer

  • Download edittitle.phpΔ and copy to your cookbook directory.
  • Add the following line to your config.php file:
          include_once("$FarmD/cookbook/edittitle.php");
As of version 1.0 the $EditTitle = 1; is not nesseccary. The functions are enabled by default. (See Optional Configuration below)
          (:input e_title:)
Upgrade Notice: Previous to version 0.9 you had to provide a label. This is now automaticly added and can be translated from the phrase "Title" in your translation table. Anyone upgrading from a previous version should remove the label from Site.EditForm to avoid the label displaying twice (remove '$[Title]' from the markup). See Fmt Variables below for additional customization.
  • Save your changes and edit titles to your hearts content.

Optional Configuration

  • To display those titles on your pages add the following where you want the Title to appear in the template file (path/to/skin/SkinName.tmpl) for the skin you are using (adjust to fit in with your skin as necessary):
          <h1>$Titlespaced</h1>
  • If you prefer using wiki markup add the following somewhere in your markup:
          !{$Titlespaced}
  • If you want to require your users to always enter a page title add the following anywhere in config.php:
          $ForceTitle = 1;
  • To temporarily turn off either $EditTitle or $ForceTitle set to zero:
          $EditTitle = 0;
          $ForceTitle = 0;
  • You may also set $EditTitle or $ForceTitle for individual groups or pages in their respective config files. See LocalCustomizations for more details.
  • In the event that you would like to force your users to include a title on each page, but prefer the (:title:) markup rather than the input field, the following settings should work:
          $EditTitle = 0;
          $ForceTitle = 1;

Fmt Variables

The following variables are provided, allowing a site admin to alter the HTML generated by the script.

  • $EditTitleInputFormat
This defines the HTML of the input field and its label that will appear in the edit form. The default is set as follows:
          $EditTitleInputFmt = "<label>\$[Title]: <input type='text' \$InputFormArgs /></label>";
  • $ForceTitleMessageFmt
This defines the error message as well as the HTML it's wrapped in when a title is not provided. The default is set as follows:
          $ForceTitleMessageFmt = '<h3 class="wikimessage">A Title must be defined for this page</h3>';
  • $EditTitlePattern
This it the regular expression pattern used to find, remove, and check for the (:title:) markup. It is set to be identical to the pattern used to define the (:title:) markup by PmWiki and there should be no reason to alter this variable.

Notes

Reasoning Behind Approach of Code (or Why did you do it that way?)

In order to maintain backward compatibility (things should still work if this recipe is disabled or removed) we don't want to change the way PmWiki stores and manages Page Titles. Therefore, before displaying the page content in the edit form, this script strips the "(:title ..." markup, displays the Title input field with the current title and the page content in the textarea without the "(:title ..." markup. Then, on save (or 'save and edit' or 'preview') the "(:title ..." markup is added back in (with new edited title) before PmWiki saves (or displays the preview of) the changed content. Upon disabling EditTitle, the "(:title ..." markup will again display in the edit form and work as previously.

An additional benefit of doing things this way, is that any changes to the Title are included in the version history of the page. Reverting to a previous Title is as simple as viewing the page history and restoring the previous state. If we were to save the Title separate from the page content, this would not be possible without also building a versioning system (or at least branching the existing one) for the Title alone.

Possible Improvements:

  • Add code to force users to enter a Title (as an option to turn on/off). <== Done (v 0.3)
  • Add ability to set ForceTitle for individual groups/pages independent of rest of recipe. <== Done (v 0.4)
  • Clean up the RemoveTitle function so it uses only one regex call. <== Done (v 0.5)
  • Add changes to the $PageEditFmt in case there is no Site.EditForm.<== Depreciated
  • Define a $Title global variable to use in the php code (see previous item).<== Depreciated
  • Add (:keyword:) markup to specify location of the title rather than editing template (thanks Sergey)Not necessary. Use !{$Titlespaced} instead. (See comments below)
  • Get ForceTitle to work independent of EditTitle. <== Done (v 1.0)

Prerequisite

  • This recipe was last tested on PmWiki version: 2.0.12
  • This recipe requires at least PmWiki version: 2.0.x and no other recipes
  • This recipe, version 1.1 was installed here 9 November, 2005

Releases

  • v 1.1Δ Waylan November 09, 2005, at 02:06 PM
    • ForceTitle is now aware of the $DeleteKeyPattern variable.
    • Set the (:title:) matching pattern ($EditTitlePattern) to be case-insensitive by default which is inline with PmWiki's default behavior.
  • v 1.0Δ Waylan November 04, 2005, at 11:05 AM
    • Now uses PmWiki's isEnabled() function, making toggles easier ($EditTitle is on and $ForceTitle off by default).
    • ForceTitle now works completely independant of EditTitle.
    • Added Fmt variables allowing additional customization by site admins.
  • v 0.9Δ Waylan October 28, 2005, at 01:30 PM
    • Fixed incompatibility with EditTemplates (See comments below).
    • Added input field label as built in feature (as a real html <label>).
  • v 0.8Δ Waylan October 27, 2005, at 10:56 AM
    • Added $EditTitle toggle - turn on or off for individual groups or pages.
    • Fix bug with page deletion - you can now delete pages when using ForceTitle.
  • v 0.7Δ (14 Oct, 2005) Waylan
    • Simplified RemoveTitle() function to use $new[title] to fill input field
  • v 0.6 (never released - listed for consistency) Waylan
  • v 0.5Δ (13 Oct, 2005) Waylan
    • Removed need to redefine the HandleEdit() function.
    • Rewrote RemoveTitle() function to only use one regex call rather than two.
    • 95 lines of code down to 53!! :P
  • v 0.4Δ (12 Oct, 2005) Waylan
    • Increased ForceTitle functionality (set for any page or group).
    • Defined default input width (set to 60).
    • Added AddTitle function bringing saving the Title inline with PmWiki ways (no ugly hacks to $_POST vars).
  • v 0.3 (11 Oct, 2005) Waylan
    • Added ability to ForceTitle with on/off toggle
  • v 0.2 (11 Oct, 2005) Waylan
    • Stopped the "(:title :)" markup from being added on save when the input field is left blank.
  • v 0.1 (11 Oct, 2005) Waylan
    • Usable but ugly in parts.

Comments

  • Is it possible to make the same thing with "(:keywords :)" markup? (Sergey P., 11 Oct, 2005)
I image that would be no big deal. Although I think we need to define a global variable (as I listed under" Possible Improvements") to call in the markup. What markup would you propose? Perhaps (:title:) with no spaces? Although that could confuse some. Maybe (:pagetitle:)? In any event, I added your suggestion to the list. (Waylan, 11 Oct, 2005)
On further investigation, adding some sort of "(:keywords :)" markup is not necessary as something similar already exists; namely MarkupVariables. Basicly, any of the variables that can be called in the templates can also be called in markup like so: {$VariableName}. So to display the title within the markup just use {$Titlespaced}. I should note that as PmWiki already offers an easy to use solution, I will not be developing one myself. Additionally, such markup should not be dependent on the EditTitle recipe and the EditTitle recipe should not be dependent on said markup being present. Of course, if anyone wants to add the markup to their own config, they certainly can. (Waylan, 18 Oct, 2005)
  • How about adding a single field for comma separated Categories which are then stored as [[!cat1]],[[!cat2]],.... (DaveG, 4-Dec-05 02:16.)
  • When used with PmWiki 2.2.0.16, the title isn't saved with the rest of the input data. I'm trying to figure this out and so far, cannot work around the problem. Does somebody face the same problem? (ThuanH, Dec. 27, 2006)

Issues

  • With this recipe, page cannot be deleted. When it is to be deleted, title is written back and page remains accessible. (Roman 2005-10-25)
    • How are you deleting the page? Try removing all the content and the title from the input field, then save. Although, I suppose if you're using ForceTitle, that is a problem. I'll see about adding a check for this. (Waylan October 27, 2005, at 10:05 AM)
    • Fixed in v 0.8. Thanks Roman.
    • How to Delete a Page: Just follow the same procedure as before. Make sure to remove the text from the Title input field (it should be blank), replace all the text in the textarea with the text "delete" and save. The page should now be 'deleted' per PmWiki's default behavior. (Waylan October 27, 2005, at 10:56 AM)
  • When combined with editTemplates new page does not get title specified in page template. (Roman 2005-10-25)
    • I believe this is because the EditTitle script is running before PmWiki looks at the $EditTemplatesFmt variable. I'll have to take another look at the code and see it I can't work around this. Although, I don't really see the value as every new page would then have the same title which seems kind of silly to me. Unless the title was something like "Add title here". Even then, its a trivial matter IMO. When I have the time, I'll see what I can do. (Waylan October 27, 2005, at 10:04 AM)
      • It looks silly but it can happen easily. You can have several groups (in my case each group for each software module) where all groups should have some pages with the same structure (I created edit templates for them). So each page created from template has the same name because they are in different groups. Roman 2005-10-28
    • Fixed in v 0.9. Thanks Roman.
    • Actually, the EditTemplate() function (which uses $EditTemplatesFmt) was (and still is) being called before my RemoveTitle() function. I was very particular about the order in which all the $EditFunctions where called to avoid breaking anything. Unfortunately, the EditTemplate() function is not aware of the title variable ($new['title'] - which this recipe adds), so it wasn't copying it over with the main text ($new['text']). The good news is that the title markup is still in the main text at this point, so I just added a catch to pull the title from the markup rather than $new['title'] if its blank. Although I had to add a second regex call (which I initially worked so hard to eliminate), it's only being called when creating a new page from an edit template (or any other situation where the core may not be aware of $new['title']). I can live with that. However, I wonder if the EditTemplate() function should be rewritten to be aware of other possible variables such as $new['title']. Patrick, what are your thoughts?

See Also

Contributors

Waylan

Edit - History - Print - Recent Changes - Search
Page last modified on June 04, 2008, at 04:54 PM