function MenuTreeStorage::setParents

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Menu/MenuTreeStorage.php \Drupal\Core\Menu\MenuTreeStorage::setParents()
  2. 10 core/lib/Drupal/Core/Menu/MenuTreeStorage.php \Drupal\Core\Menu\MenuTreeStorage::setParents()
  3. 11.x core/lib/Drupal/Core/Menu/MenuTreeStorage.php \Drupal\Core\Menu\MenuTreeStorage::setParents()

Sets the materialized path field values based on the parent.

Parameters

array $fields: The menu link.

array|false $parent: The parent menu link.

array $original: The original menu link.

1 call to MenuTreeStorage::setParents()
MenuTreeStorage::preSave in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Fills in all the fields the database save needs, using the link definition.

File

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

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

protected function setParents(array &$fields, $parent, array $original) {
    // Directly fill parents for top-level links.
    if (empty($fields['parent'])) {
        $fields['p1'] = $fields['mlid'];
        for ($i = 2; $i <= $this->maxDepth(); $i++) {
            $fields["p{$i}"] = 0;
        }
        $fields['depth'] = 1;
    }
    else {
        // @todo We want to also check $original['has_children'] here, but that
        //   will be 0 even if there are children if those are not enabled.
        //   has_children is really just the rendering hint. So, we either need
        //   to define another column (has_any_children), or do the extra query.
        //   https://www.drupal.org/node/2302149
        if ($original) {
            $limit = $this->maxDepth() - $this->doFindChildrenRelativeDepth($original) - 1;
        }
        else {
            $limit = $this->maxDepth() - 1;
        }
        if ($parent['depth'] > $limit) {
            throw new PluginException("The link with ID {$fields['id']} or its children exceeded the maximum depth of {$this->maxDepth()}");
        }
        $fields['depth'] = $parent['depth'] + 1;
        $i = 1;
        while ($i < $fields['depth']) {
            $p = 'p' . $i++;
            $fields[$p] = $parent[$p];
        }
        $p = 'p' . $i++;
        // The parent (p1 - p9) corresponding to the depth always equals the mlid.
        $fields[$p] = $fields['mlid'];
        while ($i <= static::MAX_DEPTH) {
            $p = 'p' . $i++;
            $fields[$p] = 0;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.