function EntityDisplayModeFormBase::form

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

Overrides EntityForm::form

File

core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php, line 84

Class

EntityDisplayModeFormBase
Provides the generic base class for entity display mode forms.

Namespace

Drupal\field_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
    $form['label'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Name'),
        '#maxlength' => 100,
        '#default_value' => $this->entity
            ->label(),
        '#required' => TRUE,
    ];
    $form['description'] = [
        '#title' => $this->t('Description'),
        '#type' => 'textarea',
        '#default_value' => $this->entity
            ->getDescription(),
        '#description' => $this->t('This text will be displayed on the @mode_label list page.', [
            '@mode_label' => $this->entity
                ->getEntityType()
                ->getPluralLabel(),
        ]),
    ];
    $form['id'] = [
        '#type' => 'machine_name',
        '#description' => $this->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
        '#disabled' => !$this->entity
            ->isNew(),
        '#default_value' => $this->entity
            ->id(),
        '#field_prefix' => $this->entity
            ->isNew() ? $this->entity
            ->getTargetType() . '.' : '',
        '#machine_name' => [
            'exists' => [
                $this,
                'exists',
            ],
            'replace_pattern' => '[^a-z0-9_.]+',
        ],
    ];
    $bundle_info_service = $this->entityTypeBundleInfo;
    $bundles = $bundle_info_service->getAllBundleInfo();
    $definition = $this->entityTypeManager
        ->getDefinition($this->entity
        ->isNew() ? $this->targetEntityTypeId : $this->entity
        ->getTargetType());
    $bundles_by_entity = [];
    $defaults = [];
    foreach (array_keys($bundles[$definition->id()]) as $bundle) {
        $bundles_by_entity[$bundle] = $bundles[$definition->id()][$bundle]['label'];
        // Determine default display modes.
        if (!$this->entity
            ->isNew()) {
            [
                ,
                $display_mode_name,
            ] = explode('.', $this->entity
                ->id());
            if ($this->getDisplayByContext($bundle, $display_mode_name)) {
                $defaults[$bundle] = $bundle;
            }
        }
    }
    $form['bundles_by_entity'] = [
        '#type' => 'checkboxes',
        '#title' => $this->t('Enable this @display-mode for the following @bundle-label types:', [
            '@display-mode' => $this->entityType
                ->getSingularLabel(),
            '@bundle-label' => $definition->getLabel(),
        ]),
        '#description' => $this->t('This @display-mode will still be available for the rest of the @bundle-label types if not checked here, but it will not be enabled by default.', [
            '@bundle-label' => $definition->getLabel(),
            '@display-mode' => $this->entityType
                ->getSingularLabel(),
        ]),
        '#options' => $bundles_by_entity,
        '#default_value' => $defaults,
    ];
    return $form;
}

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