function EntityDisplayFormBase::buildFieldRow

Same name and namespace in other branches
  1. 8.9.x core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::buildFieldRow()
  2. 10 core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::buildFieldRow()
  3. 11.x core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::buildFieldRow()

Builds the table row structure for a single field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array A table row array.

3 calls to EntityDisplayFormBase::buildFieldRow()
EntityDisplayFormBase::form in core/modules/field_ui/src/Form/EntityDisplayFormBase.php
Gets the actual form array to be built.
EntityFormDisplayEditForm::buildFieldRow in core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php
Builds the table row structure for a single field.
EntityViewDisplayEditForm::buildFieldRow in core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php
Builds the table row structure for a single field.
2 methods override EntityDisplayFormBase::buildFieldRow()
EntityFormDisplayEditForm::buildFieldRow in core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php
Builds the table row structure for a single field.
EntityViewDisplayEditForm::buildFieldRow in core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php
Builds the table row structure for a single field.

File

core/modules/field_ui/src/Form/EntityDisplayFormBase.php, line 295

Class

EntityDisplayFormBase
Base class for EntityDisplay edit forms.

Namespace

Drupal\field_ui\Form

Code

protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
    $field_name = $field_definition->getName();
    $display_options = $this->entity
        ->getComponent($field_name);
    $label = $field_definition->getLabel();
    // Disable fields without any applicable plugins.
    if (empty($this->getApplicablePluginOptions($field_definition))) {
        $this->entity
            ->removeComponent($field_name)
            ->save();
        $display_options = $this->entity
            ->getComponent($field_name);
    }
    $regions = array_keys($this->getRegions());
    $field_row = [
        '#attributes' => [
            'class' => [
                'draggable',
                'tabledrag-leaf',
            ],
        ],
        '#row_type' => 'field',
        '#region_callback' => [
            $this,
            'getRowRegion',
        ],
        '#js_settings' => [
            'rowHandler' => 'field',
            'defaultPlugin' => $this->getDefaultPlugin($field_definition->getType()),
        ],
        'human_name' => [
            '#plain_text' => $label,
        ],
        'weight' => [
            '#type' => 'textfield',
            '#title' => $this->t('Weight for @title', [
                '@title' => $label,
            ]),
            '#title_display' => 'invisible',
            '#default_value' => $display_options ? $display_options['weight'] : '0',
            '#size' => 3,
            '#attributes' => [
                'class' => [
                    'field-weight',
                ],
            ],
        ],
        'parent_wrapper' => [
            'parent' => [
                '#type' => 'select',
                '#title' => $this->t('Label display for @title', [
                    '@title' => $label,
                ]),
                '#title_display' => 'invisible',
                '#options' => array_combine($regions, $regions),
                '#empty_value' => '',
                '#attributes' => [
                    'class' => [
                        'js-field-parent',
                        'field-parent',
                    ],
                ],
                '#parents' => [
                    'fields',
                    $field_name,
                    'parent',
                ],
            ],
            'hidden_name' => [
                '#type' => 'hidden',
                '#default_value' => $field_name,
                '#attributes' => [
                    'class' => [
                        'field-name',
                    ],
                ],
            ],
        ],
        'region' => [
            '#type' => 'select',
            '#title' => $this->t('Region for @title', [
                '@title' => $label,
            ]),
            '#title_display' => 'invisible',
            '#options' => $this->getRegionOptions(),
            '#default_value' => $display_options ? $display_options['region'] : 'hidden',
            '#attributes' => [
                'class' => [
                    'field-region',
                ],
            ],
        ],
    ];
    $field_row['plugin'] = [
        'type' => [
            '#type' => 'select',
            '#title' => $this->t('Plugin for @title', [
                '@title' => $label,
            ]),
            '#title_display' => 'invisible',
            '#options' => $this->getApplicablePluginOptions($field_definition),
            '#default_value' => $display_options ? $display_options['type'] : 'hidden',
            '#parents' => [
                'fields',
                $field_name,
                'type',
            ],
            '#attributes' => [
                'class' => [
                    'field-plugin-type',
                ],
            ],
        ],
        'settings_edit_form' => [],
    ];
    // Get the corresponding plugin object.
    $plugin = $this->entity
        ->getRenderer($field_name);
    // Base button element for the various plugin settings actions.
    $base_button = [
        '#submit' => [
            '::multistepSubmit',
        ],
        '#ajax' => [
            'callback' => '::multistepAjax',
            'wrapper' => 'field-display-overview-wrapper',
            'effect' => 'fade',
        ],
        '#field_name' => $field_name,
    ];
    if ($form_state->get('plugin_settings_edit') == $field_name) {
        // We are currently editing this field's plugin settings. Display the
        // settings form and submit buttons.
        $field_row['plugin']['settings_edit_form'] = [];
        if ($plugin) {
            // Generate the settings form and allow other modules to alter it.
            $settings_form = $plugin->settingsForm($form, $form_state);
            $third_party_settings_form = $this->thirdPartySettingsForm($plugin, $field_definition, $form, $form_state);
            if ($settings_form || $third_party_settings_form) {
                $field_row['plugin']['#cell_attributes'] = [
                    'colspan' => 3,
                ];
                $field_row['plugin']['settings_edit_form'] = [
                    '#type' => 'container',
                    '#attributes' => [
                        'class' => [
                            'field-plugin-settings-edit-form',
                        ],
                    ],
                    '#parents' => [
                        'fields',
                        $field_name,
                        'settings_edit_form',
                    ],
                    'label' => [
                        '#markup' => $this->t('Plugin settings'),
                    ],
                    'settings' => $settings_form,
                    'third_party_settings' => $third_party_settings_form,
                    'actions' => [
                        '#type' => 'actions',
                        'save_settings' => $base_button + [
                            '#type' => 'submit',
                            '#button_type' => 'primary',
                            '#name' => $field_name . '_plugin_settings_update',
                            '#value' => $this->t('Update'),
                            '#op' => 'update',
                        ],
                        'cancel_settings' => $base_button + [
                            '#type' => 'submit',
                            '#name' => $field_name . '_plugin_settings_cancel',
                            '#value' => $this->t('Cancel'),
                            '#op' => 'cancel',
                            // Do not check errors for the 'Cancel' button, but make sure we
                            // get the value of the 'plugin type' select.
'#limit_validation_errors' => [
                                [
                                    'fields',
                                    $field_name,
                                    'type',
                                ],
                            ],
                        ],
                    ],
                ];
                $field_row['#attributes']['class'][] = 'field-plugin-settings-editing';
            }
        }
    }
    else {
        $field_row['settings_summary'] = [];
        $field_row['settings_edit'] = [];
        if ($plugin) {
            // Display a summary of the current plugin settings, and (if the
            // summary is not empty) a button to edit them.
            $summary = $plugin->settingsSummary();
            // Allow other modules to alter the summary.
            $this->alterSettingsSummary($summary, $plugin, $field_definition);
            if (!empty($summary)) {
                $field_row['settings_summary'] = [
                    '#type' => 'inline_template',
                    '#template' => '<div class="field-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
                    '#context' => [
                        'summary' => $summary,
                    ],
                    '#cell_attributes' => [
                        'class' => [
                            'field-plugin-summary-cell',
                        ],
                    ],
                ];
            }
            // Check selected plugin settings to display edit link or not.
            $settings_form = $plugin->settingsForm($form, $form_state);
            $third_party_settings_form = $this->thirdPartySettingsForm($plugin, $field_definition, $form, $form_state);
            if (!empty($settings_form) || !empty($third_party_settings_form)) {
                $field_row['settings_edit'] = $base_button + [
                    '#type' => 'image_button',
                    '#name' => $field_name . '_settings_edit',
                    '#src' => 'core/misc/icons/787878/cog.svg',
                    '#attributes' => [
                        'class' => [
                            'field-plugin-settings-edit',
                        ],
                        'alt' => $this->t('Edit'),
                    ],
                    '#op' => 'edit',
                    // Do not check errors for the 'Edit' button, but make sure we get
                    // the value of the 'plugin type' select.
'#limit_validation_errors' => [
                        [
                            'fields',
                            $field_name,
                            'type',
                        ],
                    ],
                    '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
                    '#suffix' => '</div>',
                ];
            }
        }
    }
    return $field_row;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.