8 – can’t see `div` elements added to $form in hook_form_FORM_ID_alter?


I want to modify the $form variable in an implementation of hook_form_FORM_ID_alter but can’t seem to be able to set the #prefix and #suffix variables on field widget.

/**
 * @file
 * Implementations Module Hooks
 */

use DrupalCoreFormFormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alter the node/create form for implementation types
 */
function implementations_form_node_implementation_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  $prefix = '<div style="display:none;">';
  $suffix = '</div>';

  $form('field_parent_idea')('widget')('#prefix') = $prefix;
  $form('field_parent_idea')('widget')('#suffix') = $suffix;

  dpm($form('field_parent_idea')('widget'));
  dpm($form('field_parent_idea')('widget')('#prefix'));
  dpm($form('field_parent_idea')('widget')('#suffix'));
}

When I navigate to node/add/implementation I would expect to see the value of $form('field_parent_idea')('widget') with the #prefix and #suffix variables set, but instead they’re blank. Here’s the relevant snippet showing the suffix/prefix values:

(#max_delta) => 0
(#prefix) => 
(#suffix) => 
(add_more) => Array

Why are they blank?

This is the full output of what I see.


Array
(
    (0) => Array
        (
            (target_id) => Array
                (
                    (#title) => stdClass Object
                        (
                            (__CLASS__) => DrupalCoreStringTranslationTranslatableMarkup
                            (translatedMarkup:protected) => 
                            (options:protected) => Array
                                (
                                )

                            (stringTranslation:protected) => stdClass Object
                                (
                                    (__CLASS__) => DrupalCoreStringTranslationTranslationManager
                                    (translators:protected) => Array
                                        (
                                            (30) => Array(1)
                                        )

                                    (sortedTranslators:protected) => Array
                                        (
                                            (0) => DrupalCoreStringTranslationTranslatorCustomStrings
                                        )

                                    (defaultLangcode:protected) => en
                                    (_serviceId) => string_translation
                                )

                            (string:protected) => @title (value @number)
                            (arguments:protected) => Array
                                (
                                    (@title) => Parent Idea
                                    (@number) => 1
                                )

                        )

                    (#title_display) => invisible
                    (#description) => 
                    (#field_parents) => Array
                        (
                        )

                    (#required) => 1
                    (#delta) => 0
                    (#weight) => 0
                    (#type) => entity_autocomplete
                    (#target_type) => node
                    (#selection_handler) => default:node
                    (#selection_settings) => Array
                        (
                            (target_bundles) => Array
                                (
                                    (idea) => idea
                                )

                            (sort) => Array
                                (
                                    (field) => _none
                                )

                            (auto_create) => 
                            (auto_create_bundle) => 
                            (match_operator) => CONTAINS
                            (match_limit) => 10
                        )

                    (#validate_reference) => 
                    (#maxlength) => 1024
                    (#default_value) => 
                    (#size) => 60
                    (#placeholder) => 
                )

            (_weight) => Array
                (
                    (#type) => weight
                    (#title) => stdClass Object
                        (
                            (__CLASS__) => DrupalCoreStringTranslationTranslatableMarkup
                            (translatedMarkup:protected) => 
                            (options:protected) => Array
                                (
                                )

                            (stringTranslation:protected) => stdClass Object
                                (
                                    (__CLASS__) => DrupalCoreStringTranslationTranslationManager
                                    (translators:protected) => Array
                                        (
                                            (30) => Array(1)
                                        )

                                    (sortedTranslators:protected) => Array
                                        (
                                            (0) => DrupalCoreStringTranslationTranslatorCustomStrings
                                        )

                                    (defaultLangcode:protected) => en
                                    (_serviceId) => string_translation
                                )

                            (string:protected) => Weight for row @number
                            (arguments:protected) => Array
                                (
                                    (@number) => 1
                                )

                        )

                    (#title_display) => invisible
                    (#delta) => 0
                    (#default_value) => 0
                    (#weight) => 100
                )

        )

    (#theme) => field_multiple_value_form
    (#field_name) => field_parent_idea
    (#cardinality) => -1
    (#cardinality_multiple) => 1
    (#required) => 1
    (#title) => Parent Idea
    (#description) => 
    (#max_delta) => 0
    (#prefix) => 

    (#suffix) => 

    (add_more) => Array
        (
            (#type) => submit
            (#name) => field_parent_idea_add_more
            (#value) => stdClass Object
                (
                    (__CLASS__) => DrupalCoreStringTranslationTranslatableMarkup
                    (translatedMarkup:protected) => 
                    (options:protected) => Array
                        (
                        )

                    (stringTranslation:protected) => 
                    (string:protected) => Add another item
                    (arguments:protected) => Array
                        (
                        )

                )

            (#attributes) => Array
                (
                    (class) => Array
                        (
                            (0) => field-add-more-submit
                        )

                )

            (#limit_validation_errors) => Array
                (
                    (0) => Array
                        (
                            (0) => field_parent_idea
                        )

                )

            (#submit) => Array
                (
                    (0) => Array
                        (
                            (0) => DrupalCoreFieldPluginFieldFieldWidgetEntityReferenceAutocompleteWidget
                            (1) => addMoreSubmit
                        )

                )

            (#ajax) => Array
                (
                    (callback) => Array
                        (
                            (0) => DrupalCoreFieldPluginFieldFieldWidgetEntityReferenceAutocompleteWidget
                            (1) => addMoreAjax
                        )

                    (wrapper) => field-parent-idea-add-more-wrapper
                    (effect) => fade
                )

        )

    (#after_build) => Array
        (
            (0) => Array
                (
                    (0) => DrupalCoreFieldPluginFieldFieldWidgetEntityReferenceAutocompleteWidget
                    (1) => afterBuild
                )

        )

    (#field_parents) => Array
        (
        )

    (#parents) => Array
        (
            (0) => field_parent_idea
        )

    (#tree) => 1
)

At first I thought that the issue was because I was setting on a nested array variable, but that’s exactly what the example code in the documentation does.


function hook_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  if (isset($form('type')) && $form('type')('#value') . '_node_settings' == $form_id) {
    $upload_enabled_types = Drupal::config('mymodule.settings')
      ->get('upload_enabled_types');
    $form('workflow')('upload_' . $form('type')('#value')) = array(
      '#type' => 'radios',
      '#title' => t('Attachments'),
      '#default_value' => in_array($form('type')('#value'), $upload_enabled_types) ? 1 : 0,
      '#options' => array(
        t('Disabled'),
        t('Enabled'),
      ),
    );

    // Add a custom submit handler to save the array of types back to the config file.
    $form('actions')('submit')('#submit')() = 'mymodule_upload_enabled_types_submit';
  }
}