entity_hierarchy-8.x-2.24/src/Query/HierarchyInfo.php
src/Query/HierarchyInfo.php
<?php declare(strict_types=1); namespace Drupal\entity_hierarchy\Query; /** * Defines a class for a select query that returns depths for use in a subquery. */ final class HierarchyInfo extends TreeSelectBase { /** * Gets depth SQL. * * @return string * SQL. */ protected function getTreeRootSql(): string { $table_name = $this->tables['entity']; $data_table = $this->tables['data'] ?: $this->tables['base']; $entity_id = $this->columns['entity_id']; $langcode = $this->columns['langcode']; $column_id = $this->columns['id']; $column_weight = $this->columns['weight']; $revision_id_base = $this->columns['revision_id_base']; $revision_id = $this->columns['revision_id']; $column_target_id = $this->columns['target_id']; $revision_base = ''; $revision_recurse = ''; if ($revision_id_base) { $revision_base = ",\nancestors_base.$revision_id_base AS revision_id"; $revision_recurse = ",\nancestors_base.$revision_id"; } $sql = <<<CTESQL WITH RECURSIVE ancestors AS ( SELECT ancestors_base.$entity_id AS id, eh_first.$column_target_id AS targetId, ancestors_base.$entity_id as root_id, ancestors_base.$langcode as langcode, 0 as depth, CONCAT(CHAR(length(CONV(ancestors_base.$entity_id, 10, 36)) + ORD('0') -1 ), CONV(ancestors_base.$entity_id, 10, 36)) as ordinal$revision_base FROM {{$data_table}} as ancestors_base LEFT JOIN {{$table_name}} eh_first ON eh_first.$column_id = ancestors_base.$entity_id AND eh_first.langcode = ancestors_base.langcode WHERE eh_first.$column_id IS NULL UNION ALL SELECT ancestors_base.$column_id, ancestors_base.$column_target_id, ancestors.root_id, ancestors_base.langcode, ancestors.depth + 1, CONCAT(ancestors.ordinal, '/', CONCAT(CHAR(length(CONV(100 + $column_weight, 10, 36)) + ORD('0') -1 ), CONV(100+ $column_weight, 10, 36)), '/', CONCAT(CHAR(length(CONV(ancestors_base.$column_target_id, 10, 36)) + ORD('0') -1 ), CONV(ancestors_base.$column_target_id, 10, 36)))$revision_recurse FROM {{$table_name}} ancestors_base JOIN ancestors ON ancestors_base.$column_target_id=ancestors.id AND ancestors_base.langcode=ancestors.langcode ) CTESQL; return $sql; } /** * {@inheritdoc} */ public function __toString(): string { if ($this->columns['revision_id_base']) { return $this->getTreeRootSql() . "SELECT id, root_id, langcode, revision_id, depth, ordinal FROM ancestors"; } return $this->getTreeRootSql() . "SELECT id, root_id, langcode, depth, ordinal FROM ancestors"; } }