You are here

class BreadcrumbBuilder in Filebrowser 8.2

Same name and namespace in other branches
  1. 3.x src/Breadcrumb/BreadcrumbBuilder.php \Drupal\filebrowser\Breadcrumb\BreadcrumbBuilder

Hierarchy

Expanded class hierarchy of BreadcrumbBuilder

1 string reference to 'BreadcrumbBuilder'
filebrowser.services.yml in ./filebrowser.services.yml
filebrowser.services.yml
1 service uses BreadcrumbBuilder
filebrowser.breadcrumb in ./filebrowser.services.yml
Drupal\filebrowser\Breadcrumb\BreadcrumbBuilder

File

src/Breadcrumb/BreadcrumbBuilder.php, line 14

Namespace

Drupal\filebrowser\Breadcrumb
View source
class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * @var FilebrowserStorage
   */
  protected $storage;

  /**
   * @var Common
   */
  protected $common;

  /**
   * @var \Drupal\node\NodeInterface
   *
   */
  protected $node;
  public function __construct(FilebrowserStorage $storage, Common $common) {
    $this->storage = $storage;
    $this->common = $common;
  }

  /**
   * @var \Drupal\Node\NodeInterface $node
   * @inheritdoc
   */
  public function applies(RouteMatchInterface $route_match) {
    $this->node = $this->common
      ->getNodeFromPath();
    return isset($this->node) && $this->node
      ->bundle() == 'dir_listing';
  }

  /**
   * @inheritdoc
   */
  public function build(RouteMatchInterface $route_match) {

    /** @var \Drupal\node\NodeInterface $node */
    $breadcrumb = new Breadcrumb();
    $this->node = $this->common
      ->getNodeFromPath();
    $title = $this->node
      ->getTitle();
    $fid = \Drupal::request()->query
      ->get('fid');
    if (isset($fid)) {
      $content = $this->storage
        ->loadAllRecordsFromRoot($this->node
        ->id());
    }
    else {
      $content = !empty($content) ? $content : [
        'path' => '/',
      ];
    }
    $links = $this
      ->buildLinks($title, $content, $fid);
    $breadcrumb
      ->setLinks($links);
    $breadcrumb
      ->addCacheContexts([
      'url',
      'url.query_args',
    ]);
    return $breadcrumb;
  }

  /**
   * Creates the filebrowser breadcrumb links
   * @param string $title
   * @param int $fid id of current folder being viewed
   * @param array $content array from the DB containing all paths (folders) keyed by $fid.
   * @return array
   */
  private function buildLinks($title, $content, $fid) {
    $links[0] = Link::createFromRoute($this
      ->t('Home'), '<front>');
    $links[1] = Link::createFromRoute($title, 'entity.node.canonical', [
      'node' => $this->node
        ->id(),
    ]);
    $trail = isset($fid) && isset($content[$fid]) ? ltrim($content[$fid]->path, "/") : null;
    $folders_raw = !is_null($trail) ? explode('/', $trail) : null;
    if (!empty($folders_raw)) {

      // process the folder to set the fid
      $folders = $this
        ->processTrail($folders_raw, $content);
      $count = count($folders) + 1;
      for ($i = 2; $i <= $count; $i++) {
        if ($i < $count) {
          $links[$i] = Link::fromTextAndUrl($folders[$i - 2]['title'], Url::fromUserInput('/node'));
          $links[$i] = Link::fromTextAndUrl($folders[$i - 2]['title'], Url::fromRoute('entity.node.canonical', [
            'node' => $this->node
              ->id(),
          ], [
            'query' => [
              'fid' => $folders[$i - 2]['fid'],
            ],
          ]));
        }
        else {
          $links[$i] = Link::createFromRoute($folders[$i - 2]['title'], '<none>');
        }
      }
    }
    else {

      // there are no subdirectories so [1] is the last item
      // route the link to <none>
      $links[1] = Link::createFromRoute($title, '<none>');
    }
    return $links;
  }
  protected function processTrail($folders, $content) {
    $result = [];
    $count = count($folders);
    for ($i = 0; $i < $count; $i++) {
      $folder_path = '/' . implode('/', array_slice($folders, 0, $i + 1));
      $result[$i]['title'] = $folders[$i];
      $result[$i]['folder_path'] = $folder_path;

      // Loop trough $content and search for the own and parent fid
      foreach ($content as $fid => $row) {
        if ($row->path == $folder_path) {
          $result[$i]['fid'] = $row->fid;
        }
      }
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BreadcrumbBuilder::$common protected property
BreadcrumbBuilder::$node protected property
BreadcrumbBuilder::$storage protected property
BreadcrumbBuilder::applies public function @inheritdoc Overrides BreadcrumbBuilderInterface::applies
BreadcrumbBuilder::build public function @inheritdoc Overrides BreadcrumbBuilderInterface::build
BreadcrumbBuilder::buildLinks private function Creates the filebrowser breadcrumb links
BreadcrumbBuilder::processTrail protected function
BreadcrumbBuilder::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.