1

I have content type with a taxonomy attached to it (since it is Drupal 8, it ends up being an Entity Reference field). The form is configured to render out this field input as checkboxes.

I have run into a situation where I need to change some of the checkboxes into plain text when the form is rendered (node create, note edit etc). I tried to push a #process function onto the field,

function MYMODULE_form_node_job_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['field_foo']['widget']['#process'][] = 'MYMODULE_foo';
}

function MYMODULE_foo(&$element, FormStateInterface $form_state, &$complete_form) {
  // do stuff
}

but when I inspect the $element with XDebug, the #options haven't been processed yet.

So, where in the form / render pipeline can this be changed?

4
  • I need to change some of the check boxes into plain text You didn't say explicitly when? when the form is viewed? submitted? or...??? Could affect the answer.
    – No Sssweat
    May 30, 2017 at 22:56
  • You could just unset them and add a new form key with #markup.
    – No Sssweat
    May 31, 2017 at 0:10
  • @NoSssweat Thanks. I would normally do that, but the question is a huge simplification of my actual problem. Essentially I need the top level terms available for reference in the form in the order that they are on the taxonomy page (like headings), but only for certain roles. Other roles always get the checkboxes. Lots of fine-grained access control on this site; its more web-app than pure CMS.
    – mpdonadio
    May 31, 2017 at 0:51
  • Can't you solve that using an if statement... if ($current_logged_in_user_role = 'some_role') { unset($form['some-checkbox']); } ?
    – No Sssweat
    May 31, 2017 at 1:10

1 Answer 1

3

Beside preprocess you can add code to the rendering of form elements in #pre_render, #theme, #theme_wrappers and #post_render. Part of the rendering pipeline of checkboxes is for example CompositeFormElementTrait::preRenderCompositeFormElement:

  public static function preRenderCompositeFormElement($element) {
    // Set the element's title attribute to show #title as a tooltip, if needed.
    if (isset($element['#title']) && $element['#title_display'] == 'attribute') {
      $element['#attributes']['title'] = $element['#title'];
      if (!empty($element['#required'])) {
        // Append an indication that this field is required.
        $element['#attributes']['title'] .= ' (' . t('Required') . ')';
      }
    }

    if (isset($element['#title']) || isset($element['#description'])) {
      // @see #type 'fieldgroup'
      $element['#attributes']['id'] = $element['#id'] . '--wrapper';
      $element['#theme_wrappers'][] = 'fieldset';
      $element['#attributes']['class'][] = 'fieldgroup';
      $element['#attributes']['class'][] = 'form-composite';
    }
    return $element;

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.