Version 1
[yaffs-website] / web / core / modules / field_ui / src / Form / EntityDisplayModeAddForm.php
1 <?php
2
3 namespace Drupal\field_ui\Form;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
8 /**
9  * Provides the add form for entity display modes.
10  */
11 class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
12
13   /**
14    * The entity type for which the display mode is being created.
15    *
16    * @var string
17    */
18   protected $targetEntityTypeId;
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
24     $this->targetEntityTypeId = $entity_type_id;
25     $form = parent::buildForm($form, $form_state);
26     // Change replace_pattern to avoid undesired dots.
27     $form['id']['#machine_name']['replace_pattern'] = '[^a-z0-9_]+';
28     $definition = $this->entityManager->getDefinition($this->targetEntityTypeId);
29     $form['#title'] = $this->t('Add new %label @entity-type', ['%label' => $definition->getLabel(), '@entity-type' => $this->entityType->getLowercaseLabel()]);
30     return $form;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function validateForm(array &$form, FormStateInterface $form_state) {
37     parent::validateForm($form, $form_state);
38
39     $form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id'));
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function prepareEntity() {
46     $definition = $this->entityManager->getDefinition($this->targetEntityTypeId);
47     if (!$definition->get('field_ui_base_route') || !$definition->hasViewBuilderClass()) {
48       throw new NotFoundHttpException();
49     }
50
51     $this->entity->setTargetType($this->targetEntityTypeId);
52   }
53
54 }