entity_generic-8.x-3.x-dev/src/Form/GenericTypeForm.php
src/Form/GenericTypeForm.php
<?php namespace Drupal\entity_generic\Form; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\BundleEntityFormBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\field_ui\FieldUI; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for entity type forms. */ class GenericTypeForm extends BundleEntityFormBase { /** * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * Creates a class instance. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity manager. */ public function __construct(EntityTypeManagerInterface $entity_type_manager) { $this->entityTypeManager = $entity_type_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('entity_type.manager') ); } /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $entity_definition = $this->entityTypeManager->getDefinition($this->entity->getEntityType()->get('bundle_of')); if ($this->operation == 'add') { $form['#title'] = $this->t('Add %type', ['%type' => $this->entity->getEntityType()->getLabel()]); } else { $form['#title'] = $this->t('Edit %label %type', [ '%label' => $this->entity->label(), '%type' => $this->entity->getEntityType()->getLabel(), ]); } $form['label'] = [ '#title' => t('Label'), '#type' => 'textfield', '#default_value' => $this->entity->label(), '#description' => t('The human-readable name of this entity type.'), '#required' => TRUE, '#size' => 30, '#weight' => 0, ]; $form['id'] = [ '#type' => 'machine_name', '#default_value' => $this->entity->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => !$this->entity->isNew(), '#machine_name' => [ 'exists' => [$this, 'exists'], ], '#description' => t('A unique machine-readable name for this entity type. It must only contain lowercase letters, numbers, and underscores.'), '#weight' => 1, ]; $form['description'] = array( '#title' => t('Description'), '#type' => 'textarea', '#default_value' => $this->entity->getDescription(), '#description' => t('This text will be displayed on the <em>Add new entity</em> page.'), ); $form['additional_settings'] = [ '#type' => 'vertical_tabs', ]; $form['submission'] = [ '#type' => 'details', '#title' => t('Submission form settings'), '#group' => 'additional_settings', '#open' => TRUE, ]; $form['submission']['help'] = [ '#title' => $this->t('Explanation or submission guideline'), '#type' => 'textarea', '#default_value' => $this->entity->getHelp(), '#description' => $this->t('This text will be displayed at the top of the page when creating or editing of <em>@type @entity</em>.', ['@type' => $this->entity->label(), '@entity' => $entity_definition->getLabel()]), ]; $form['workflow'] = [ '#type' => 'details', '#title' => t('Workflow options'), '#group' => 'additional_settings', ]; $workflow_options = []; $workflow_display_options = []; if ($entity_definition->hasKey('status')) { $workflow_options['status'] = $this->entity->get('status'); $workflow_display_options['status'] = t('Status'); } if ($entity_definition->isRevisionable()) { $workflow_options['revision'] = $this->entity->shouldCreateNewRevision(); $workflow_display_options['revision'] = t('Create new revision'); } $workflow_keys = array_keys(array_filter($workflow_options)); $workflow_options = array_combine($workflow_keys, $workflow_keys); $form['workflow']['options'] = [ '#type' => 'checkboxes', '#title' => t('Default options'), '#default_value' => $workflow_options, '#options' => $workflow_display_options, '#description' => t('Users with the <em>Administer @type</em> permission will be able to override these options.', ['@type' => $this->entity->label()]), ]; return $form; } /** * {@inheritdoc} */ protected function actions(array $form, FormStateInterface $form_state) { $actions = parent::actions($form, $form_state); if (\Drupal::moduleHandler()->moduleExists('field_ui') && $this->getEntity()->isNew() ) { $actions['submit']['#value'] = t('Save entity type'); $actions['save_continue'] = $actions['submit']; $actions['save_continue']['#value'] = t('Save and manage fields'); $actions['save_continue']['#submit'][] = [$this, 'redirectToFieldUI']; $actions['delete']['#value'] = t('Delete entity type'); } return $actions; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $id = trim($form_state->getValue('id')); // '0' is invalid, since elsewhere we check it using empty(). if ($id == '0') { $form_state->setErrorByName('id', $this->t('Invalid machine-readable name. Enter a name other than %invalid.', array('%invalid' => $id))); } } /** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $entity_definition = $this->entityTypeManager->getDefinition($this->entity->getEntityType()->get('bundle_of')); if ($entity_definition->hasKey('status')) { $this->entity->set('status', $form_state->getValue(['options', 'status'])); } if ($entity_definition->isRevisionable()) { $this->entity->setNewRevision($form_state->getValue(['options', 'revision'])); } $status = $this->entity->save(); $messenger = \Drupal::messenger(); if ($status == SAVED_UPDATED) { $messenger->addMessage(t('%type %label has been updated.', [ '%label' => $this->entity->label(), '%type' => $this->entity->getEntityType()->getLabel(), ])); } else { $messenger->addMessage(t('%type %label has been created.', [ '%label' => $this->entity->label(), '%type' => $this->entity->getEntityType()->getLabel(), ])); } $form_state->setRedirect('entity.' . $this->entity->getEntityType()->id() . '.collection'); } /** * Form submission handler to redirect to Manage fields page of Field UI. * * @param array $form * Form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * Form state. */ public function redirectToFieldUI(array $form, FormStateInterface $form_state) { if ($form_state->getTriggeringElement()['#parents'][0] === 'save_continue' && $route_info = FieldUI::getOverviewRouteInfo($this->entity->getEntityType()->getBundleOf(), $this->entity->id())) { $form_state->setRedirectUrl($route_info); } } /** * Check whether the entity type exists. * * @param int $id * Entity id to check. * * @return bool * TRUE if entity exits. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ public function exists($id) { return !empty($this->entityTypeManager->getStorage($this->entity->getEntityType()->id())->load($id)); } }