Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Service helper functions you should place in your service class:

Code Block
languagephp
  /**
   * Helper function to get siblings of the node if sibling relationship exists.
   *
   * @param Node $node
   *    node interface.
   *
   * @return array $siblings
   *    array of node siblings ids.
   */
  public function getSiblings(Node $node) {
    $siblings = [];
    if (isset($node->field_master_node)) {
      $master = $node->field_master_node->target_id;
      if (isset($master)) {
        $siblings = $this->getChildNodes($master);
      }
    }
    return $siblings;
  }
  /**
   * Helper function to get node aliases and language / region prefix by nid.
   *
   * @param array $nids
   *    Node ids.
   *
   * @return array $aliases
   *    Array of aliases.
   */
  public function getAliases($nids) {
    $languages = \Drupal::languageManager()->getLanguages();
    if (!is_array($nids)) {
      $nids = [$nids];
    }
    $aliases = [];
    $i = 0;
    foreach ($nids as $nid) {
      foreach ($languages as $lng_code => $lang) {
        $alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $nid, $lng_code);
        if (strpos($alias, '/node/') === FALSE) {
          $al_arr = explode('/', $alias);
          $alias = '/' . $lng_code . $alias;
          $aliases[$i]['link'] = $alias;
  
          $aliases[$i]['lang'] = $al_arr[1] . '-' . $lng_code;
          $i++;
        }
      }
    }
    return $aliases;
  }

...