You are here

protected static function MenuCreator::createMenuLink in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 src/Import/MenuCreator.php \Drupal\gathercontent\Import\MenuCreator::createMenuLink()

Create menu link if requested.

Parameters

int $nid: ID of \Drupal\node\NodeInterface object.

string $title: Title for menu link.

string $plid: Parent menu link ID, null if we don't want to create menu link.

null|string $lang: Langcode for menu link.

null|int $original_link_id: ID of menu link item in default language.

1 call to MenuCreator::createMenuLink()
MenuCreator::createMenu in src/Import/MenuCreator.php
Create a menu link to the imported node.

File

src/Import/MenuCreator.php, line 51

Class

MenuCreator
Class for creating menus after import.

Namespace

Drupal\gathercontent\Import

Code

protected static function createMenuLink($nid, $title, $plid, $lang = NULL, &$original_link_id = NULL) {
  $weight = 1;
  if (!empty($plid)) {
    if (is_null($lang)) {

      // Single language node.
      list($menu_name, $mlid) = explode(':', $plid);

      // Get parent menu link ID.
      if ($menu_name === 'node') {
        static::getMenuByGcId($mlid, $menu_name);
      }
      $link = [
        'link' => [
          'uri' => 'entity:node/' . $nid,
        ],
        'title' => $title,
        'menu_name' => $menu_name,
        'parent' => $mlid,
      ];
      MenuLinkContent::create($link)
        ->set('weight', $weight)
        ->save();
    }
    elseif (static::isMenuTranslatable()) {
      if (!is_null($lang) && is_null($original_link_id)) {

        // Multi language node - first language.
        list($menu_name, $mlid) = explode(':', $plid);

        // Get parent menu link ID.
        if ($menu_name === 'node') {
          static::getMenuByGcId($mlid, $menu_name, $lang);
        }
        $link = [
          'link' => [
            'uri' => 'entity:node/' . $nid,
          ],
          'title' => $title,
          'menu_name' => $menu_name,
          'parent' => $mlid,
          'langcode' => $lang,
        ];
        $menu_link = MenuLinkContent::create($link);
        $menu_link
          ->set('weight', $weight);
        $menu_link
          ->save();
        $original_link_id = $menu_link
          ->id();
      }
      elseif (!is_null($lang) && !is_null($original_link_id)) {

        // Multi language node - other language.
        list($menu_name, $mlid) = explode(':', $plid);
        if ($menu_name === 'node') {
          static::getMenuByGcId($mlid, $menu_name, $lang);
        }
        $link = [
          'link' => [
            'uri' => 'entity:node/' . $nid,
          ],
          'title' => $title,
          'menu_name' => $menu_name,
          'parent' => $mlid,
          'langcode' => $lang,
        ];

        // Load parent item.
        $original_item = MenuLinkContent::load($original_link_id);
        $original_item
          ->addTranslation($lang, $link);
        $original_item
          ->save();
      }
    }
  }
}