Pull merge.
[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  * @internal
12  */
13 class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
14
15   /**
16    * The entity type for which the display mode is being created.
17    *
18    * @var string
19    */
20   protected $targetEntityTypeId;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
26     $this->targetEntityTypeId = $entity_type_id;
27     $form = parent::buildForm($form, $form_state);
28     // Change replace_pattern to avoid undesired dots.
29     $form['id']['#machine_name']['replace_pattern'] = '[^a-z0-9_]+';
30     $definition = $this->entityManager->getDefinition($this->targetEntityTypeId);
31     $form['#title'] = $this->t('Add new %label @entity-type', ['%label' => $definition->getLabel(), '@entity-type' => $this->entityType->getLowercaseLabel()]);
32     return $form;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function validateForm(array &$form, FormStateInterface $form_state) {
39     parent::validateForm($form, $form_state);
40
41     $form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id'));
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function prepareEntity() {
48     $definition = $this->entityManager->getDefinition($this->targetEntityTypeId);
49     if (!$definition->get('field_ui_base_route') || !$definition->hasViewBuilderClass()) {
50       throw new NotFoundHttpException();
51     }
52
53     $this->entity->setTargetType($this->targetEntityTypeId);
54   }
55
56 }