class MenuForm in Colossal Menu 8
Same name and namespace in other branches
- 2.x src/Form/MenuForm.php \Drupal\colossal_menu\Form\MenuForm
Settings form for menus.
@package Drupal\colossal_menu\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\colossal_menu\Form\MenuForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of MenuForm
File
- src/
Form/ MenuForm.php, line 19
Namespace
Drupal\colossal_menu\FormView source
class MenuForm extends EntityForm {
/**
* Entity Type Manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Menu Tree.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected $menuLinkTree;
/**
* Constructor.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, MenuLinkTreeInterface $menu_link_tree) {
$this->entityTypeManager = $entity_type_manager;
$this->menuLinkTree = $menu_link_tree;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('colossal_menu.link_tree'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$menu = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $menu
->label(),
'#description' => $this
->t("Label for the Menu."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $menu
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\colossal_menu\\Entity\\Menu::load',
],
'#disabled' => !$menu
->isNew(),
];
// Add menu links administration form for existing menus.
if (!$menu
->isNew()) {
// Form API supports constructing and validating self-contained sections
// within forms, but does not allow handling the form section's submission
// equally separated yet. Therefore, we use a $form_state key to point to
// the parents of the form section.
// @see self::submitOverviewForm()
$form_state
->set('links', [
'links',
]);
$form['links'] = [];
$form['links'] = $this
->buildOverviewForm($form['links'], $form_state);
}
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$menu = $this->entity;
if (!$menu
->isNew()) {
$this
->submitOverviewForm($form, $form_state);
}
$status = $menu
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addStatus($this
->t('Created the %label Menu.', [
'%label' => $menu
->label(),
]));
break;
default:
$this
->messenger()
->addStatus($this
->t('Saved the %label Menu.', [
'%label' => $menu
->label(),
]));
}
$form_state
->setRedirectUrl($menu
->toUrl('collection'));
}
/**
* Submit handler for the menu overview form.
*
* This function takes great care in saving parent items first, then items
* underneath them. Saving items in the incorrect order can break the tree.
*/
protected function submitOverviewForm(array $complete_form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
foreach ($input['links'] as $id => $input) {
$storage = $this->entityTypeManager
->getStorage('colossal_menu_link');
/** @var \Drupal\colossal_menu\Entity\Link $link */
$link = $storage
->load($id);
$diff = FALSE;
if (array_key_exists('parent', $input) && $link
->get('parent')
->access('edit')) {
if (!$link
->getParent() && $input['parent']) {
$diff = TRUE;
$link
->setParent($input['parent']);
}
elseif ($link
->getParent() && $link
->getParent()
->id() != $input['parent']) {
$diff = TRUE;
$link
->setParent($input['parent']);
}
}
if (array_key_exists('weight', $input) && $link
->get('weight')
->access('edit')) {
if ($link
->getWeight() != $input['weight']) {
$diff = TRUE;
$link
->setWeight($input['weight']);
}
}
if (array_key_exists('enabled', $input) && $link
->get('enabled')
->access('edit')) {
$enabled = (bool) $input['enabled'];
if ($link
->isEnabled() != $enabled) {
$diff = TRUE;
$link
->setEnabled($enabled);
}
}
if ($diff) {
$link
->save();
}
}
}
/**
* Form constructor to edit an entire menu tree at once.
*
* Shows for one menu the menu links accessible to the current user and
* relevant operations.
*
* This form constructor can be integrated as a section into another form. It
* relies on the following keys in $form_state:
* - menu: A menu entity.
* - menu_overview_form_parents: An array containing the parent keys to this
* form.
* Forms integrating this section should call menu_overview_form_submit() from
* their form submit handler.
*/
protected function buildOverviewForm(array &$form, FormStateInterface $form_state) {
$menu = $this->entity;
// Ensure that menu_overview_form_submit() knows the parents of this form
// section.
if (!$form_state
->has('menu_overview_form_parents')) {
$form_state
->set('menu_overview_form_parents', []);
}
$form['links'] = [
'#type' => 'table',
'#sorted' => TRUE,
'#header' => [
$this
->t('Title'),
$this
->t('Enabled'),
$this
->t('Weight'),
[
'data' => $this
->t('Operations'),
'colspan' => 3,
],
],
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'link-parent',
'subgroup' => 'link-parent',
'source' => 'link-id',
'hidden' => TRUE,
'limit' => $this->menuLinkTree
->maxDepth(),
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'link-weight',
],
],
];
$params = new MenuTreeParameters();
$tree = $this->menuLinkTree
->load($menu
->id(), $params);
$elements = [];
foreach ($tree as $item) {
$this
->buildLinkElement($elements, $item);
}
$form['links'] = $form['links'] + $elements;
return $form;
}
/**
* Build an array of link elements.
*
* @param array $elements
* An array of form elements to be filled.
* @param \Drupal\Core\Menu\MenuLinkTreeElement $item
* Menu Link Tree element.
* @param int $depth
* The current depth.
*/
protected function buildLinkElement(array &$elements, MenuLinkTreeElement $item, $depth = 0) {
/** @var \Drupal\colossal_menu\Entity\Link $link */
$link = $item->link;
$id = $link
->id();
$elements[$id] = [
'#weight' => $link
->getWeight(),
];
if ($link
->get('parent')
->access('edit') && $link
->get('weight')
->access('edit')) {
$elements[$id]['#attributes']['class'][] = 'draggable';
}
$text = [];
if (!$link
->isExternal() && $link
->getRouteName() == '<none>') {
$text = [
'#plain_text' => $link
->getTitle(),
];
}
else {
$text = Link::fromTextAndUrl($link
->getTitle(), $link
->getUrlObject())
->toRenderable();
}
$elements[$id]['indent'] = [
[
'#theme' => 'indentation',
'#size' => $depth,
],
[
$text,
],
];
if ($link
->get('enabled')
->access('edit')) {
$elements[$id]['enabled'] = [
'#type' => 'checkbox',
'#default_value' => $link
->isEnabled(),
'#title' => $this
->t('Enabled'),
'#title_display' => 'invisible',
];
}
else {
$elements[$id]['enabled'] = [
'#markup' => $link
->isEnabled() ? $this
->t('Enabled') : $this
->t('Disabled'),
];
}
$elements[$id]['weight'] = [
'#type' => 'weight',
'#delta' => 50,
'#default_value' => $link
->getWeight(),
'#title' => $this
->t('Weight for @title', [
'@title' => $link
->getTitle(),
]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'link-weight',
],
],
'#access' => $link
->get('weight')
->access('edit'),
];
$operations = [];
if ($link
->access('update')) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => $link
->getEditRoute(),
'query' => $this
->getDestinationArray(),
];
}
if ($link
->access('delete')) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => $link
->getDeleteRoute(),
'query' => $this
->getDestinationArray(),
];
}
$elements[$id]['operations'] = [
'#type' => 'operations',
'#links' => $operations,
];
$elements[$id]['id'] = [
'#type' => 'hidden',
'#default_value' => $id,
'#attributes' => [
'class' => [
'link-id',
],
],
];
$elements[$id]['parent'] = [
'#type' => 'hidden',
'#default_value' => $link
->getParent() ? $link
->getParent()
->id() : 0,
'#attributes' => [
'class' => [
'link-parent',
],
],
'#access' => $link
->get('parent')
->access('edit'),
];
if (!empty($item->subtree)) {
$subdepth = $depth + 1;
foreach ($item->subtree as $subitem) {
$this
->buildLinkElement($elements, $subitem, $subdepth);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MenuForm:: |
protected | property |
Entity Type Manager. Overrides EntityForm:: |
|
MenuForm:: |
protected | property | Menu Tree. | |
MenuForm:: |
protected | function | Build an array of link elements. | |
MenuForm:: |
protected | function | Form constructor to edit an entire menu tree at once. | |
MenuForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MenuForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
MenuForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
MenuForm:: |
protected | function | Submit handler for the menu overview form. | |
MenuForm:: |
public | function | Constructor. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |