You are here

protected function MenuTreeStorage::preSave in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Menu/MenuTreeStorage.php \Drupal\Core\Menu\MenuTreeStorage::preSave()

Fills in all the fields the database save needs, using the link definition.

Parameters

array $link: The link definition to be updated.

array $original: The link definition before the changes. May be empty if not found.

Return value

array The values which will be stored.

Throws

\Drupal\Component\Plugin\Exception\PluginException Thrown when the specific depth exceeds the maximum.

1 call to MenuTreeStorage::preSave()
MenuTreeStorage::doSave in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Saves a link without clearing caches.

File

core/lib/Drupal/Core/Menu/MenuTreeStorage.php, line 355

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

protected function preSave(array &$link, array $original) {
  static $schema_fields, $schema_defaults;
  if (empty($schema_fields)) {
    $schema = static::schemaDefinition();
    $schema_fields = $schema['fields'];
    foreach ($schema_fields as $name => $spec) {
      if (isset($spec['default'])) {
        $schema_defaults[$name] = $spec['default'];
      }
    }
  }

  // Try to find a parent link. If found, assign it and derive its menu.
  $parent = $this
    ->findParent($link, $original);
  if ($parent) {
    $link['parent'] = $parent['id'];
    $link['menu_name'] = $parent['menu_name'];
  }
  else {
    $link['parent'] = '';
  }

  // If no corresponding parent link was found, move the link to the
  // top-level.
  foreach ($schema_defaults as $name => $default) {
    if (!isset($link[$name])) {
      $link[$name] = $default;
    }
  }
  $fields = array_intersect_key($link, $schema_fields);

  // Sort the route parameters so that the query string will be the same.
  asort($fields['route_parameters']);

  // Since this will be urlencoded, it's safe to store and match against a
  // text field.
  $fields['route_param_key'] = $fields['route_parameters'] ? UrlHelper::buildQuery($fields['route_parameters']) : '';
  foreach ($this
    ->serializedFields() as $name) {
    if (isset($fields[$name])) {
      $fields[$name] = serialize($fields[$name]);
    }
  }
  $this
    ->setParents($fields, $parent, $original);

  // Need to check both parent and menu_name, since parent can be empty in any
  // menu.
  if ($original && ($link['parent'] != $original['parent'] || $link['menu_name'] != $original['menu_name'])) {
    $this
      ->moveChildren($fields, $original);
  }

  // We needed the mlid above, but not in the update query.
  unset($fields['mlid']);

  // Cast Booleans to int, if needed.
  $fields['enabled'] = (int) $fields['enabled'];
  $fields['expanded'] = (int) $fields['expanded'];
  return $fields;
}