MenuLinkContentForm.php 4.79 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\menu_link_content\Form;

5
use Drupal\Component\Datetime\TimeInterface;
6 7
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
8
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9
use Drupal\Core\Form\FormStateInterface;
10 11
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Menu\MenuParentFormSelectorInterface;
12
use Drupal\Core\Path\PathValidatorInterface;
13 14 15 16
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form to add/update content menu links.
17 18
 *
 * @internal
19
 */
20
class MenuLinkContentForm extends ContentEntityForm {
21 22 23 24

  /**
   * The content menu link.
   *
25
   * @var \Drupal\menu_link_content\MenuLinkContentInterface
26 27 28 29 30 31 32 33 34 35
   */
  protected $entity;

  /**
   * The parent form selector service.
   *
   * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
   */
  protected $menuParentSelector;

36 37 38 39 40 41 42
  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

43 44 45 46 47 48 49 50 51
  /**
   * Constructs a MenuLinkContentForm object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
   *   The menu parent form selector service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
52 53
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
54 55 56 57
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
58
   */
59 60
  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) {
    parent::__construct($entity_manager, $entity_type_bundle_info, $time);
61
    $this->menuParentSelector = $menu_parent_selector;
62
    $this->pathValidator = $path_validator;
63 64 65 66 67 68 69 70 71 72
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
      $container->get('menu.parent_form_selector'),
      $container->get('language_manager'),
73 74 75
      $container->get('path.validator'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time')
76 77 78 79 80 81
    );
  }

  /**
   * {@inheritdoc}
   */
82
  public function form(array $form, FormStateInterface $form_state) {
83 84 85
    $form = parent::form($form, $form_state);

    $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
86 87
    $id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
    $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id);
88 89 90 91 92 93 94 95 96 97 98
    $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;
  }

  /**
   * {@inheritdoc}
   */
99
  protected function actions(array $form, FormStateInterface $form_state) {
100 101 102 103 104 105 106 107 108 109
    $element = parent::actions($form, $form_state);
    $element['submit']['#button_type'] = 'primary';
    $element['delete']['#access'] = $this->entity->access('delete');

    return $element;
  }

  /**
   * {@inheritdoc}
   */
110
  public function buildEntity(array $form, FormStateInterface $form_state) {
111
    /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
112 113
    $entity = parent::buildEntity($form, $form_state);

114 115 116 117
    list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);

    $entity->parent->value = $parent;
    $entity->menu_name->value = $menu_name;
118 119
    $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
    $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value']));
120 121 122 123 124 125 126

    return $entity;
  }

  /**
   * {@inheritdoc}
   */
127
  public function save(array $form, FormStateInterface $form_state) {
128 129 130 131 132 133
    // The entity is rebuilt in parent::submit().
    $menu_link = $this->entity;
    $saved = $menu_link->save();

    if ($saved) {
      drupal_set_message($this->t('The menu link has been saved.'));
134 135
      $form_state->setRedirect(
        'entity.menu_link_content.canonical',
136
        ['menu_link_content' => $menu_link->id()]
137 138 139 140
      );
    }
    else {
      drupal_set_message($this->t('There was an error saving the menu link.'), 'error');
141
      $form_state->setRebuild();
142 143 144 145
    }
  }

}