3 namespace Drupal\field_ui\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
9 * Provides the generic base class for entity display mode forms.
11 abstract class EntityDisplayModeFormBase extends EntityForm {
14 * The entity type definition.
16 * @var \Drupal\Core\Entity\EntityTypeInterface
18 protected $entityType;
23 protected function init(FormStateInterface $form_state) {
24 parent::init($form_state);
25 $this->entityType = $this->entityTypeManager->getDefinition($this->entity->getEntityTypeId());
31 public function form(array $form, FormStateInterface $form_state) {
33 '#type' => 'textfield',
34 '#title' => $this->t('Name'),
36 '#default_value' => $this->entity->label(),
40 '#type' => 'machine_name',
41 '#description' => $this->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
42 '#disabled' => !$this->entity->isNew(),
43 '#default_value' => $this->entity->id(),
44 '#field_prefix' => $this->entity->isNew() ? $this->entity->getTargetType() . '.' : '',
46 'exists' => [$this, 'exists'],
47 'replace_pattern' => '[^a-z0-9_.]+',
55 * Determines if the display mode already exists.
57 * @param string|int $entity_id
59 * @param array $element
63 * TRUE if the display mode exists, FALSE otherwise.
65 public function exists($entity_id, array $element) {
66 // Do not allow to add internal 'default' view mode.
67 if ($entity_id == 'default') {
70 return (bool) $this->entityTypeManager
71 ->getStorage($this->entity->getEntityTypeId())
73 ->condition('id', $element['#field_prefix'] . $entity_id)
80 public function save(array $form, FormStateInterface $form_state) {
81 drupal_set_message($this->t('Saved the %label @entity-type.', ['%label' => $this->entity->label(), '@entity-type' => $this->entityType->getLowercaseLabel()]));
82 $this->entity->save();
83 \Drupal::entityManager()->clearCachedFieldDefinitions();
84 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));