Hello,
I have build a small form to allow some users to add a new taxonomy term to a vocabulary. To do so, I request the user to select a vocabulary (and the form collect the vid of the vocabulary), to input the new term and a description.
But anytime I access the form, I am receiving a notice:
Notice: Undefined index: #value in forum_form_alter() (line 585 of drupal/modules/forum/forum.module)
which is linked to the following code in forum.module:
/**
* Implements hook_form_alter().
*/
function forum_form_alter(&$form, $form_state, $form_id) {
$vid = variable_get('forum_nav_vocabulary', 0);
if (isset($form['vid']) && $form['vid']['#value'] == $vid) {
// Hide critical options from forum vocabulary.
if ($form_id == 'taxonomy_form_vocabulary') {
$form['help_forum_vocab'] = array(
The main issue is that in my form, I allow the user to select a vocabulary, so form['vid'] does exists ... but since it is a user input parameter, there is no form['vid']['#value'] ... hence the notice.
There are several issues here:
- The forum module should not assume that because forum['vid'] exists, it means that forum['vid']['#value'] exists. It should test for both.
- When writing a form alter, you should check the context (such as checking that you are targeting the correct form_id, and presence of some values) as soon as possible, so that clashes with other modules defining hook_form_alter will be avoided. This is clearly not the case here in this particular hook. The checks are done later.
I have implemented a fix for 1 on my install, but really what should be done is fix 2 and testing that it is not having side effects (if the hook is written like that at present, maybe there is a reason).
S.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | 1275514-8-forum_hook_form_alter.patch | 2.12 KB | twistor |
| #7 | forum_hook_form_alter-v1.1.patch | 2.26 KB | spouilly |
| #1 | forum_hook_form_alter.patch | 2.17 KB | spouilly |
Comments
Comment #1
spouilly commentedOk, finally decided to write a proper patch for issue 2, so here it is. The patch probably still need some work because the hook still has some statements that falls outside of the scope of the form_id (meaning it will apply to any/all forms) and I suspect it is wrong, but I don't have much time to investigate the issue deeper.
Comment #2
larowlanHi, good catch on this one.
Let's see what the bot says.
Comment #3
larowlanThis should use a capital T for test and end with a period. Can we have a comment here describing what these form alter changes are for - ie to prevent the user from making changes to the vocab/term that would make it incompatible with how forum module works.
This patch will collide with #10566: Forum can not appear in multiple containers, we'll need to be careful not to revert it's changes.
Powered by Dreditor.
Comment #5
spouilly commentedHi there,
I am new to contribute officially to Drupal, do you want me to rewrite the patch so that it follows the drupal coding standard, or do you take over to investigate the impact of changing the hook_form_alter ?
S.
Comment #6
larowlanHappy for you to repost
Comment #7
spouilly commentedSorry it took me so long ... here is a new version of the patch (against drupal 7.7). The code is the same, I have simply updated the comment to reflect your proposition (explaining what the hook_form_alter does), and removed my own comment because it makes only sense to explain between the "before" and "after" (so it is useful to explain the patch itself, but not as a documentation of the code).
The patch does not address the potential collision with the other patch mentioned in #3
Comment #8
twistor commentedRe-rolled the patch in the correct format. Having this problem in D6.
Comment #9
twistor commentedComment #11
BeaPower commentedsub - have error message here: /admin/structure/custom_breadcrumbs/taxonomy_vocabulary/add
Comment #12
alexweber commentedI have looked at the code from 8.x branch and this error no longer occurs.
Can someone double-check this, it doesn't look like we need a patch at all for this as it's already fixed!
Comment #13
develcuy commentedAdding tag "dlatino" for reference of the Drupal Latino community.
Comment #14
langelhc commentedThe problem is already resolved in Drupal 8.x because there are two specifics form alters for these forms:
forum_form_taxonomy_form_term_alter(), forum_form_taxonomy_form_vocabulary_alter().
The fix should be backported to Drupal 7.x.
Comment #15
heilop commentedThis issue is fixed at #787652: Forum vocabulary alterations possibly obsolete -- possible to delete forum vocab.