0

I'm using Drupal 7 and Advanced forum.

The users when will create a new topic will be able to select the forum container and forums, but if they select a container, an error will be showed. I want to avoid this by altering that taxonomy options with adding an #attribute or #disabled to specific forum container.

As I inspect the elements, this is the option that I want to change:

$form['taxonomy_forums']['und']['#options'][334];

First of all I've tried to test by adding a class for that option like this:

$form['taxonomy_forums']['und']['#options'][334]['#attributes'] = array('class' => array('test-class'));

but that didn't worked, it simply erases the title of that option.

With #disabled it didn't work too.

From https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#attributes

I can see a good example, but also a note:

Note: If you are altering an existing form via hook_form_alter() or a similar means, be careful with this property! You will probably want to add to the existing array rather than writing over it, so don't follow this usage example exactly.

It's kind a confusing, so how can I add an #attribute or #disabled to option with hook_form_alter?

EDIT: This is a part from my code:

<?php
function custom_alter_form_alter (&$form, &$form_state, $form_id) {
   switch ($form_id) {

        case 'forum_node_form';


        $form['taxonomy_forums']['und']['#process'][] = 'custom_alter_process_checkboxes';
        krumo ($form);
        break;
    }

}

function custom_alter_process_checkboxes($element) {
  $element[334]['#attributes']['class'][] = 'test-class';
  $element[334]['#disabled'] = TRUE;
  return $element;
  krumo ($element);
}

1 Answer 1

0

Checkboxes are converted to their individual form elements in form_process_checkboxes (), so you'll need a process function:

// In the form alter...
$form['taxonomy_forums']['und']['#process'][] = 'MYMODULE_process_checkboxes';

function MYMODULE_process_checkboxes($element) {
  $element[334]['#attributes']['class'][] = 'test-class';
  $element[334]['#disabled'] = TRUE;
  return $element;
}
4
  • I've added your code and I can see the #process function in $form['taxonomy_forums']['und'], added the custom function in my module, but It didn't add any attribute to the option. Commented Jun 18, 2014 at 11:20
  • Have you confirmed the process function is being called and inspected $element, etc? It's not likely I can give you 100% working code, this is just a guide to help you on the way to writing whatever makes sense for your site and setup
    – Clive
    Commented Jun 18, 2014 at 11:21
  • I've edited the main thread. Krumo doesn't show any $element array. BTW the taxonomy is with select list. Commented Jun 18, 2014 at 11:27
  • I would guess that $form['taxonomy_forums']['und'] isn't the right element in that case. Process functions will run for any valid element they're attached to.
    – Clive
    Commented Jun 18, 2014 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.