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:

  1. The forum module should not assume that because forum['vid'] exists, it means that forum['vid']['#value'] exists. It should test for both.
  2. 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.

Comments

spouilly’s picture

Issue tags: +Forum, +hook_form_alter
StatusFileSize
new2.17 KB

Ok, 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.

larowlan’s picture

Version: 7.8 » 8.x-dev
Status: Active » Needs review
Issue tags: -Forum, -hook_form_alter +Needs backport to D7

Hi, good catch on this one.
Let's see what the bot says.

larowlan’s picture

Status: Needs review » Needs work
--- 581,605 ----
!   // test for the form_id first

This 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.

--- 581,605 ----
!         $form['advanced']['parent']['#access'] = FALSE;

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.

spouilly’s picture

Hi 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.

larowlan’s picture

Happy for you to repost

spouilly’s picture

StatusFileSize
new2.26 KB

Sorry 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

twistor’s picture

StatusFileSize
new2.12 KB

Re-rolled the patch in the correct format. Having this problem in D6.

twistor’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 1275514-8-forum_hook_form_alter.patch, failed testing.

BeaPower’s picture

sub - have error message here: /admin/structure/custom_breadcrumbs/taxonomy_vocabulary/add

alexweber’s picture

Status: Needs work » Needs review

I 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!

develcuy’s picture

Issue tags: +dlatino

Adding tag "dlatino" for reference of the Drupal Latino community.

langelhc’s picture

Version: 8.x-dev » 7.x-dev
Priority: Normal » Major
Status: Needs review » Needs work

The 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.

heilop’s picture

Status: Needs work » Closed (duplicate)