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.
11 class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
14 * The entity type for which the display mode is being created.
18 protected $targetEntityTypeId;
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()]);
36 public function validateForm(array &$form, FormStateInterface $form_state) {
37 parent::validateForm($form, $form_state);
39 $form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id'));
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();
51 $this->entity->setTargetType($this->targetEntityTypeId);