View source
<?php
namespace Drupal\entity_hierarchy_microsite\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuParentFormSelectorInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MicrositeMenuItemForm extends ContentEntityForm {
protected $entity;
protected $menuParentSelector;
protected $pathValidator;
public function __construct(EntityRepositoryInterface $entity_repository, MenuParentFormSelectorInterface $menu_parent_selector, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->menuParentSelector = $menu_parent_selector;
$this->pathValidator = $path_validator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('menu.parent_form_selector'), $container
->get('path.validator'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$default = 'entity-hierarchy-microsite:' . $this->entity
->getParent();
$id = 'entity_hierarchy_microsite:' . $this->entity
->getTarget();
$form['menu_parent'] = $this->menuParentSelector
->parentSelectElement($default, $id, [
'entity-hierarchy-microsite' => 'Microsites',
]);
$form['menu_parent']['#weight'] = 10;
$form['menu_parent']['#title'] = $this
->t('Parent link');
$form['menu_parent']['#description'] = $this
->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.');
$form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$element['submit']['#button_type'] = 'primary';
$element['delete']['#value'] = $this
->t('Remove override');
$element['delete']['#access'] = $this->entity
->access('delete');
return $element;
}
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = parent::buildEntity($form, $form_state);
list(, $parent) = explode(':', $form_state
->getValue('menu_parent'), 2);
$entity->parent->value = $parent;
$entity->enabled->value = !$form_state
->isValueEmpty([
'enabled',
'value',
]);
$entity->expanded->value = !$form_state
->isValueEmpty([
'expanded',
'value',
]);
return $entity;
}
public function save(array $form, FormStateInterface $form_state) {
$override = $this->entity;
$override
->save();
$this
->messenger()
->addStatus($this
->t('The override has been saved.'));
$form_state
->setRedirectUrl(new Url('entity.menu.edit_form', [
'menu' => 'entity-hierarchy-microsite',
]));
}
}