You are here

public function NodeOrderManager::getOrderableTidsFromNode in Node Order 8

Get all term IDs on a node that are on orderable vocabularies.

Returns an array of the node's tids that are in orderable vocabularies. Slower than self::getOrderableTids() but needed when tids have already been removed from the database.

Parameters

\Drupal\node\NodeInterface $node: The node to find term IDs for.

Return value

int[] An array of term IDs.

Overrides NodeOrderManagerInterface::getOrderableTidsFromNode

File

src/NodeOrderManager.php, line 294

Class

NodeOrderManager
Defines a service that creates & manages node ordering within taxonomy terms.

Namespace

Drupal\nodeorder

Code

public function getOrderableTidsFromNode(NodeInterface $node) {
  $tids = [];
  foreach ($node
    ->getFieldDefinitions() as $field) {
    if ($field
      ->getType() == 'entity_reference' && $field
      ->getSetting('target_type') == 'taxonomy_term') {

      // If a field value is not set in the node object when node_save() is
      // called, the old value from $node->original is used.
      $field_name = $field
        ->getName();
      foreach ($node
        ->getTranslationLanguages() as $langcode) {
        $translated = $node
          ->getTranslation($langcode
          ->getId());
        foreach ($translated->{$field_name} as $item) {
          $term = $item
            ->getValue();
          if (!empty($term['target_id'])) {
            $tids[$term['target_id']] = $term['target_id'];
          }
        }
      }
    }
  }
  return $tids;
}