diff core/modules/taxonomy/src/TermStorage.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
line wrap: on
line diff
--- a/core/modules/taxonomy/src/TermStorage.php	Thu Feb 28 13:21:36 2019 +0000
+++ b/core/modules/taxonomy/src/TermStorage.php	Thu May 09 15:33:08 2019 +0100
@@ -4,6 +4,7 @@
 
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
+use Drupal\Core\Entity\Sql\TableMappingInterface;
 
 /**
  * Defines a Controller class for taxonomy terms.
@@ -47,6 +48,19 @@
   protected $ancestors;
 
   /**
+   * The type of hierarchy allowed within a vocabulary.
+   *
+   * Possible values:
+   * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
+   * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
+   * - VocabularyInterface::HIERARCHY_MULTIPLE: Multiple parents.
+   *
+   * @var int[]
+   *   An array of one the possible values above, keyed by vocabulary ID.
+   */
+  protected $vocabularyHierarchyType;
+
+  /**
    * {@inheritdoc}
    *
    * @param array $values
@@ -72,6 +86,7 @@
     $this->treeParents = [];
     $this->treeTerms = [];
     $this->trees = [];
+    $this->vocabularyHierarchyType = [];
     parent::resetCache($ids);
   }
 
@@ -325,7 +340,7 @@
    * {@inheritdoc}
    */
   public function getNodeTerms(array $nids, array $vocabs = [], $langcode = NULL) {
-    $query = db_select($this->getDataTable(), 'td');
+    $query = $this->database->select($this->getDataTable(), 'td');
     $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
     $query->fields('td', ['tid']);
     $query->addField('tn', 'nid', 'node_nid');
@@ -360,10 +375,79 @@
   /**
    * {@inheritdoc}
    */
+  public function getTermIdsWithPendingRevisions() {
+    $table_mapping = $this->getTableMapping();
+    $id_field = $table_mapping->getColumnNames($this->entityType->getKey('id'))['value'];
+    $revision_field = $table_mapping->getColumnNames($this->entityType->getKey('revision'))['value'];
+    $rta_field = $table_mapping->getColumnNames($this->entityType->getKey('revision_translation_affected'))['value'];
+    $langcode_field = $table_mapping->getColumnNames($this->entityType->getKey('langcode'))['value'];
+    $revision_default_field = $table_mapping->getColumnNames($this->entityType->getRevisionMetadataKey('revision_default'))['value'];
+
+    $query = $this->database->select($this->getRevisionDataTable(), 'tfr');
+    $query->fields('tfr', [$id_field]);
+    $query->addExpression("MAX(tfr.$revision_field)", $revision_field);
+
+    $query->join($this->getRevisionTable(), 'tr', "tfr.$revision_field = tr.$revision_field AND tr.$revision_default_field = 0");
+
+    $inner_select = $this->database->select($this->getRevisionDataTable(), 't');
+    $inner_select->condition("t.$rta_field", '1');
+    $inner_select->fields('t', [$id_field, $langcode_field]);
+    $inner_select->addExpression("MAX(t.$revision_field)", $revision_field);
+    $inner_select
+      ->groupBy("t.$id_field")
+      ->groupBy("t.$langcode_field");
+
+    $query->join($inner_select, 'mr', "tfr.$revision_field = mr.$revision_field AND tfr.$langcode_field = mr.$langcode_field");
+
+    $query->groupBy("tfr.$id_field");
+
+    return $query->execute()->fetchAllKeyed(1, 0);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getVocabularyHierarchyType($vid) {
+    // Return early if we already computed this value.
+    if (isset($this->vocabularyHierarchyType[$vid])) {
+      return $this->vocabularyHierarchyType[$vid];
+    }
+
+    $parent_field_storage = $this->entityFieldManager->getFieldStorageDefinitions($this->entityTypeId)['parent'];
+    $table_mapping = $this->getTableMapping();
+
+    $target_id_column = $table_mapping->getFieldColumnName($parent_field_storage, 'target_id');
+    $delta_column = $table_mapping->getFieldColumnName($parent_field_storage, TableMappingInterface::DELTA);
+
+    $query = $this->database->select($table_mapping->getFieldTableName('parent'), 'p');
+    $query->addExpression("MAX($target_id_column)", 'max_parent_id');
+    $query->addExpression("MAX($delta_column)", 'max_delta');
+    $query->condition('bundle', $vid);
+
+    $result = $query->execute()->fetchAll();
+
+    // If all the terms have the same parent, the parent can only be root (0).
+    if ((int) $result[0]->max_parent_id === 0) {
+      $this->vocabularyHierarchyType[$vid] = VocabularyInterface::HIERARCHY_DISABLED;
+    }
+    // If no term has a delta higher than 0, no term has multiple parents.
+    elseif ((int) $result[0]->max_delta === 0) {
+      $this->vocabularyHierarchyType[$vid] = VocabularyInterface::HIERARCHY_SINGLE;
+    }
+    else {
+      $this->vocabularyHierarchyType[$vid] = VocabularyInterface::HIERARCHY_MULTIPLE;
+    }
+
+    return $this->vocabularyHierarchyType[$vid];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function __sleep() {
     $vars = parent::__sleep();
     // Do not serialize static cache.
-    unset($vars['ancestors'], $vars['treeChildren'], $vars['treeParents'], $vars['treeTerms'], $vars['trees']);
+    unset($vars['ancestors'], $vars['treeChildren'], $vars['treeParents'], $vars['treeTerms'], $vars['trees'], $vars['vocabularyHierarchyType']);
     return $vars;
   }
 
@@ -378,6 +462,7 @@
     $this->treeParents = [];
     $this->treeTerms = [];
     $this->trees = [];
+    $this->vocabularyHierarchyType = [];
   }
 
 }