3 namespace Drupal\field_ui\Form;
5 use Drupal\Core\Form\FormStateInterface;
6 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 * Provides the add form for entity display modes.
13 class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
16 * The entity type for which the display mode is being created.
20 protected $targetEntityTypeId;
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()]);
38 public function validateForm(array &$form, FormStateInterface $form_state) {
39 parent::validateForm($form, $form_state);
41 $form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id'));
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();
53 $this->entity->setTargetType($this->targetEntityTypeId);