You are here

class NestedSetStorageFactory in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/NestedSetStorageFactory.php \Drupal\entity_hierarchy\Storage\NestedSetStorageFactory

Defines a factory for creating a nested set storage handler for hierarchies.

Hierarchy

Expanded class hierarchy of NestedSetStorageFactory

10 files declare their use of NestedSetStorageFactory
ChildEntityWarningBuilder.php in src/Information/ChildEntityWarningBuilder.php
ChildOfMicrositeLookup.php in modules/entity_hierarchy_microsite/src/ChildOfMicrositeLookup.php
EntityHierarchy.php in src/Plugin/EntityReferenceSelection/EntityHierarchy.php
EntityHierarchy.php in modules/entity_hierarchy_workbench_access/src/Plugin/AccessControlHierarchy/EntityHierarchy.php
EntityHierarchyArgumentPluginBase.php in src/Plugin/views/argument/EntityHierarchyArgumentPluginBase.php

... See full list

1 string reference to 'NestedSetStorageFactory'
entity_hierarchy.services.yml in ./entity_hierarchy.services.yml
entity_hierarchy.services.yml
1 service uses NestedSetStorageFactory
entity_hierarchy.nested_set_storage_factory in ./entity_hierarchy.services.yml
Drupal\entity_hierarchy\Storage\NestedSetStorageFactory

File

src/Storage/NestedSetStorageFactory.php, line 12

Namespace

Drupal\entity_hierarchy\Storage
View source
class NestedSetStorageFactory {

  /**
   * DBAL connection.
   *
   * @var \Doctrine\DBAL\Connection
   */
  protected $connection;

  /**
   * Static cache.
   *
   * @var array
   */
  protected $cache = [];

  /**
   * Table prefix.
   *
   * @var string
   */
  protected $tablePrefix = '';

  /**
   * Logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a new NestedSetStorageFactory object.
   *
   * @param \Doctrine\DBAL\Connection $connection
   *   Dbal Connection.
   * @param \Drupal\Core\Database\Connection $drupalConnection
   *   Drupal connection.
   * @param \Psr\Log\LoggerInterface $logger
   *   Logger.
   */
  public function __construct(Connection $connection, DrupalConnection $drupalConnection, LoggerInterface $logger) {
    $this->connection = $connection;
    $this->tablePrefix = $drupalConnection
      ->tablePrefix();
    $this->logger = $logger;
  }

  /**
   * Gets a new nested set storage handler.
   *
   * @param string $field_name
   *   Field name for storage.
   * @param string $entity_type_id
   *   Entity Type ID.
   *
   * @return \Drupal\entity_hierarchy\Storage\NestedSetStorage
   *   Nested set for given field.
   */
  public function get($field_name, $entity_type_id) {
    $table_name = $this
      ->getTableName($field_name, $entity_type_id);
    return $this
      ->fromTableName($table_name);
  }

  /**
   * Gets a new nested set storage handler for the given table.
   *
   * @param string $table_name
   *   Table name.
   *
   * @return \Drupal\entity_hierarchy\Storage\NestedSetStorage
   *   Nested set for given field.
   *
   * @todo Remove this in favour of derivative argument plugins?
   */
  public function fromTableName($table_name) {
    if (!isset($this->cache[$table_name])) {
      $this->cache[$table_name] = new NestedSetStorage($this->connection, $table_name, $this->logger);
    }
    return $this->cache[$table_name];
  }

  /**
   * Gets table name for a given field name and entity type.
   *
   * @param string $field_name
   *   Field name.
   * @param string $entity_type_id
   *   Entity Type ID.
   * @param bool $withPrefix
   *   (optional) TRUE to add prefix. Views data does not need prefix.
   *
   * @return string
   *   Table name.
   */
  public function getTableName($field_name, $entity_type_id, $withPrefix = TRUE) {
    return sprintf('%snested_set_%s_%s', $withPrefix ? $this->tablePrefix : '', $field_name, $entity_type_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NestedSetStorageFactory::$cache protected property Static cache.
NestedSetStorageFactory::$connection protected property DBAL connection.
NestedSetStorageFactory::$logger protected property Logger.
NestedSetStorageFactory::$tablePrefix protected property Table prefix.
NestedSetStorageFactory::fromTableName public function Gets a new nested set storage handler for the given table.
NestedSetStorageFactory::get public function Gets a new nested set storage handler.
NestedSetStorageFactory::getTableName public function Gets table name for a given field name and entity type.
NestedSetStorageFactory::__construct public function Constructs a new NestedSetStorageFactory object.