This is a companion to http://drupal.org/node/286419, which describes a distinct (and much simpler) aspect of the problem I'm having.
I am using Advanced Help in a core Drupal form (the node editing page) rather than in content displayed by my own module. I accomplished this by adding a help link to the label for each field with a module_form_alter hook function.
But.
I first attempted to do this by modifying each field's #title element to include a link to help, for example:
$form['title']['#title'] .= ' ' . theme('advanced_help_topic', 'my_module', 'Title');
I expected this to display the help icon link to the right of the field's label, but a couple of weird things happened.
First: when I looked at the page source, I saw this:
<label for=...>Title <a href="/help/my_module/Title?popup=1" class="advanced-help-link" title="Title field"><span>Help</span>
</a>: <span class="form-required" title="This field is required.">*</span></label>
<input ...> ...
The anchor tag appears between the word "Title" and the following colon, right where I would expect my code to put it. Yet the anchor appears on the page at the start of the following line, before the text box. How does it get there?
(A related mystery is the anchor content, which is the text "Help" in the page source, but is the help icon on the page. I'm not too concerned about this one; it's probably just some JavaScript magic, but I'd like to know how it happens it if it can be explained easily.)
Second: when I clicked the link, I got the help text in the main window (Advanced Help did not open a pop-up window for it). By comparing my page's output to that from the Advanced Help example, which does open a pop-up window, I found that I was getting an anchor tag like the one above, while the example had an anchor tag with an onclick attribute which opened the pop-up.
I studied the code which generates the link. When I call theme() it calls theme_advanced_help_topic() in the Advanced Help module, which tests whether the user has 'view advanced help popup' permission, and creates a link with onclick if so, and a link without onclick if not.
At that point I thought that I just had to figure out why the user didn't get that permission when I generated the link. I was wrong. The user did have that permission. The theme() function returned a link with an onclick attribute, which I duly stored in $form['title']['#title']. And then Drupal rendered a page which contained an anchor tag without onclick.
This doesn't make sense. I'm missing something, but I don't know what to look for. Can anyone make suggestions?
Once I was thoroughly frustrated and stumped, I tried changing my original code to place the help icon in a prefix to the label, rather than the body of the label:
$form['title']['#prefix'] = theme('advanced_help_topic', 'my_module', 'Title');
This gave me a link that contained onclick, which opened a pop-up window as I wanted. I'm pleased that this works, but it doesn't let me put the help icon where I want it, and I don't understand why it works at all when the first approach does not.
Comments
Comment #1
orthoducks commentedI discovered another awkward aspect of this situation. Files referenced by "path:" must be in the module's Help directory. In this case, where "the module" is arbitrary, the files are forced to be in a place that is illogical from the viewpoint of the site's design.
I hope that "path:" can be made more flexible so that the files can be stored, for example, in the theme's Help directory.
Comment #2
moshe weitzman commentedI suspect that the Drupal filter system is stripping the onclick attribute because it assumes that #title should not contain such markup. There are no such restrictions on #prefix, as you saw. Closing this issue as it does not belong here.
For path, perhaps see #298934: Create a more robust path replacement.