3 namespace Drupal\menu_link_content\Form;
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityManagerInterface;
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.
18 class MenuLinkContentForm extends ContentEntityForm {
21 * The content menu link.
23 * @var \Drupal\menu_link_content\MenuLinkContentInterface
28 * The parent form selector service.
30 * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
32 protected $menuParentSelector;
37 * @var \Drupal\Core\Path\PathValidatorInterface
39 protected $pathValidator;
42 * Constructs a MenuLinkContentForm object.
44 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
46 * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
47 * The menu parent form selector service.
48 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
49 * The language manager.
50 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
52 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
53 * The entity type bundle service.
54 * @param \Drupal\Component\Datetime\TimeInterface $time
57 public function __construct(EntityManagerInterface $entity_manager, MenuParentFormSelectorInterface $menu_parent_selector, LanguageManagerInterface $language_manager, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
58 parent::__construct($entity_manager, $entity_type_bundle_info, $time);
59 $this->menuParentSelector = $menu_parent_selector;
60 $this->pathValidator = $path_validator;
66 public static function create(ContainerInterface $container) {
68 $container->get('entity.manager'),
69 $container->get('menu.parent_form_selector'),
70 $container->get('language_manager'),
71 $container->get('path.validator'),
72 $container->get('entity_type.bundle.info'),
73 $container->get('datetime.time')
80 public function form(array $form, FormStateInterface $form_state) {
81 $form = parent::form($form, $form_state);
83 $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
84 $id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
85 $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id);
86 $form['menu_parent']['#weight'] = 10;
87 $form['menu_parent']['#title'] = $this->t('Parent link');
88 $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.');
89 $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
97 protected function actions(array $form, FormStateInterface $form_state) {
98 $element = parent::actions($form, $form_state);
99 $element['submit']['#button_type'] = 'primary';
100 $element['delete']['#access'] = $this->entity->access('delete');
108 public function buildEntity(array $form, FormStateInterface $form_state) {
109 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
110 $entity = parent::buildEntity($form, $form_state);
112 list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
114 $entity->parent->value = $parent;
115 $entity->menu_name->value = $menu_name;
116 $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
117 $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value']));
125 public function save(array $form, FormStateInterface $form_state) {
126 // The entity is rebuilt in parent::submit().
127 $menu_link = $this->entity;
128 $saved = $menu_link->save();
131 drupal_set_message($this->t('The menu link has been saved.'));
132 $form_state->setRedirect(
133 'entity.menu_link_content.canonical',
134 ['menu_link_content' => $menu_link->id()]
138 drupal_set_message($this->t('There was an error saving the menu link.'), 'error');
139 $form_state->setRebuild();