You are here

class MediaDirectoriesTreeBuilder in Media Directories 3.x

Hierarchy

Expanded class hierarchy of MediaDirectoriesTreeBuilder

1 string reference to 'MediaDirectoriesTreeBuilder'
media_directories_ui.services.yml in modules/media_directories_ui/media_directories_ui.services.yml
modules/media_directories_ui/media_directories_ui.services.yml
1 service uses MediaDirectoriesTreeBuilder
media_directories_ui.tree_builder in modules/media_directories_ui/media_directories_ui.services.yml
Drupal\media_directories_ui\MediaDirectoriesTreeBuilder

File

modules/media_directories_ui/src/MediaDirectoriesTreeBuilder.php, line 8

Namespace

Drupal\media_directories_ui
View source
class MediaDirectoriesTreeBuilder {
  use StringTranslationTrait;

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\taxonomy\TermStorage
   */
  protected $termStorage;

  /**
   * The vocabulary id to use.
   *
   * @var string
   */
  protected $vocabulary_id;

  /**
   * MediaDirectoriesController constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
    $config = $this->configFactory
      ->get('media_directories.settings');
    $this->vocabulary_id = $config
      ->get('directory_taxonomy');
  }

  /**
   * Return complete directory tree.
   *
   * @return array
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function tree() {
    $tree = [];
    $this->termStorage = $this->entityTypeManager
      ->getStorage('taxonomy_term');
    $terms = $this->termStorage
      ->loadTree($this->vocabulary_id);
    foreach ($terms as $term) {
      $this
        ->buildTree($tree, $term, $this->vocabulary_id);
    }
    $tree = [
      [
        'id' => 'dir-root',
        'text' => $this
          ->t('Root'),
        'state' => [
          'opened' => TRUE,
          'selected' => TRUE,
        ],
        'a_attr' => [
          'data-tid' => MEDIA_DIRECTORY_ROOT,
        ],
        'children' => array_values($tree),
      ],
    ];
    return $tree;
  }

  /**
   * Populates a tree array given a taxonomy term tree object.
   *
   * @param $tree
   * @param $object
   * @param $vocabulary
   */
  protected function buildTree(&$tree, $object, $vocabulary) {
    if ($object->depth !== 0) {
      return;
    }
    $tree[$object->tid] = $object;
    $tree[$object->tid]->children = [];
    $tree[$object->tid]->text = $object->name;
    $tree[$object->tid]->a_attr = [
      'data-tid' => $object->tid,
    ];
    $tree[$object->tid]->id = 'dir-' . $object->tid;
    $object_children =& $tree[$object->tid]->children;
    $children = $this->termStorage
      ->loadChildren($object->tid);
    if (!$children) {
      return;
    }
    $child_tree_objects = $this->termStorage
      ->loadTree($vocabulary, $object->tid);
    foreach ($children as $child) {
      foreach ($child_tree_objects as $child_tree_object) {
        if ($child_tree_object->tid === $child
          ->id()) {
          $this
            ->buildTree($object_children, $child_tree_object, $vocabulary);
        }
      }
    }
    $tree[$object->tid]->children = array_values($tree[$object->tid]->children);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaDirectoriesTreeBuilder::$configFactory protected property
MediaDirectoriesTreeBuilder::$entityTypeManager protected property
MediaDirectoriesTreeBuilder::$termStorage protected property
MediaDirectoriesTreeBuilder::$vocabulary_id protected property The vocabulary id to use.
MediaDirectoriesTreeBuilder::buildTree protected function Populates a tree array given a taxonomy term tree object.
MediaDirectoriesTreeBuilder::tree public function Return complete directory tree.
MediaDirectoriesTreeBuilder::__construct public function MediaDirectoriesController constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.