3 namespace Drupal\menu_link_content\Form;
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Menu\MenuParentFormSelectorInterface;
12 use Drupal\Core\Path\PathValidatorInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides a form to add/update content menu links.
20 class MenuLinkContentForm extends ContentEntityForm {
23 * The content menu link.
25 * @var \Drupal\menu_link_content\MenuLinkContentInterface
30 * The parent form selector service.
32 * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
34 protected $menuParentSelector;
39 * @var \Drupal\Core\Path\PathValidatorInterface
41 protected $pathValidator;
44 * Constructs a MenuLinkContentForm object.
46 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
47 * The entity repository.
48 * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
49 * The menu parent form selector service.
50 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51 * The language manager.
52 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
54 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
55 * The entity type bundle service.
56 * @param \Drupal\Component\Datetime\TimeInterface $time
59 public function __construct(EntityRepositoryInterface $entity_repository, MenuParentFormSelectorInterface $menu_parent_selector, LanguageManagerInterface $language_manager, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
60 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
61 $this->menuParentSelector = $menu_parent_selector;
62 $this->pathValidator = $path_validator;
68 public static function create(ContainerInterface $container) {
70 $container->get('entity.repository'),
71 $container->get('menu.parent_form_selector'),
72 $container->get('language_manager'),
73 $container->get('path.validator'),
74 $container->get('entity_type.bundle.info'),
75 $container->get('datetime.time')
82 public function form(array $form, FormStateInterface $form_state) {
83 $form = parent::form($form, $form_state);
85 $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
86 $id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
87 $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id);
88 $form['menu_parent']['#weight'] = 10;
89 $form['menu_parent']['#title'] = $this->t('Parent link');
90 $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.');
91 $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
99 protected function actions(array $form, FormStateInterface $form_state) {
100 $element = parent::actions($form, $form_state);
101 $element['submit']['#button_type'] = 'primary';
102 $element['delete']['#access'] = $this->entity->access('delete');
110 public function buildEntity(array $form, FormStateInterface $form_state) {
111 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
112 $entity = parent::buildEntity($form, $form_state);
114 list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
116 $entity->parent->value = $parent;
117 $entity->menu_name->value = $menu_name;
118 $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
119 $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value']));
127 public function save(array $form, FormStateInterface $form_state) {
128 // The entity is rebuilt in parent::submit().
129 $menu_link = $this->entity;
130 $saved = $menu_link->save();
133 $this->messenger()->addStatus($this->t('The menu link has been saved.'));
134 $form_state->setRedirect(
135 'entity.menu_link_content.canonical',
136 ['menu_link_content' => $menu_link->id()]
140 $this->messenger()->addError($this->t('There was an error saving the menu link.'));
141 $form_state->setRebuild();