function EntityDisplayModeFormBase::save

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

Overrides EntityForm::save

File

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

Class

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

Namespace

Drupal\field_ui\Form

Code

public function save(array $form, FormStateInterface $form_state) {
    $this->messenger()
        ->addStatus($this->t('Saved the %label @entity-type.', [
        '%label' => $this->entity
            ->label(),
        '@entity-type' => $this->entityType
            ->getSingularLabel(),
    ]));
    $this->entity
        ->save();
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    $form_state->setRedirectUrl($this->entity
        ->toUrl('collection'));
    [
        ,
        $display_mode_name,
    ] = explode('.', $form_state->getValue('id'));
    $target_entity_id = $this->targetEntityTypeId;
    foreach ($form_state->getValue('bundles_by_entity') as $bundle => $value) {
        if (!empty($value)) {
            // Add a new entity view/form display if it doesn't already exist.
            if (!$this->getDisplayByContext($bundle, $display_mode_name)) {
                $display = $this->getEntityDisplay($target_entity_id, $bundle, 'default')
                    ->createCopy($display_mode_name);
                $display->save();
            }
            // This message is still helpful, even if the view/form display hasn't
            // changed, so we keep it outside the above check.
            $url = $this->getOverviewUrl($display_mode_name, $value);
            $bundle_info_service = $this->entityTypeBundleInfo;
            $bundles = $bundle_info_service->getAllBundleInfo();
            $bundle_label = $bundles[$target_entity_id][$bundle]['label'];
            $display_mode_label = $form_state->getValue('label');
            $this->messenger()
                ->addStatus($this->t('<a href=":url">Configure the %display_mode_label %mode mode for %bundle_label</a>.', [
                '%mode' => $this->displayContext,
                '%display_mode_label' => $display_mode_label,
                '%bundle_label' => $bundle_label,
                ':url' => $url->toString(),
            ]));
        }
        else {
            // The view/form display has been unchecked, so we need to delete this.
            // There's no confirmation of deleting the view/form display on the node
            // content type forms either, so we match that behavior.
            if ($display = $this->getDisplayByContext($bundle, $display_mode_name)) {
                $display->delete();
            }
        }
    }
}

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