You are here

function gc_create_menu_link in GatherContent 8.3

Same name and namespace in other branches
  1. 8 gathercontent.module \gc_create_menu_link()

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 gc_create_menu_link()
_gc_fetcher in ./gathercontent.module
Function for fetching, creating and updating content from GatherContent.

File

./gathercontent.module, line 387
Main module file for GatherContent module.

Code

function gc_create_menu_link($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') {
        _gc_get_menu_by_gc_id($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 (\Drupal::moduleHandler()
      ->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
      ->isEnabled('menu_link_content')) {
      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') {
          _gc_get_menu_by_gc_id($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') {
          _gc_get_menu_by_gc_id($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();
      }
    }
  }
}