You are here

public function Acquiadam::getBreadcrumb in Media: Acquia DAM 8

1 call to Acquiadam::getBreadcrumb()
Acquiadam::getForm in src/Plugin/EntityBrowser/Widget/Acquiadam.php

File

src/Plugin/EntityBrowser/Widget/Acquiadam.php, line 521

Class

Acquiadam
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\media_acquiadam\Plugin\EntityBrowser\Widget

Code

public function getBreadcrumb(Folder $current_folder, array $breadcrumbs = []) {

  // If the folder being rendered is already in the breadcrumb trail
  // and the breadcrumb trail is longer than 1 (i.e. root folder only)
  if (array_key_exists($current_folder->id, $breadcrumbs) && count($breadcrumbs) > 1) {

    // This indicates that the user has navigated "Up" the folder structure
    // 1 or more levels.
    do {

      // Go to the end of the breadcrumb array.
      end($breadcrumbs);

      // Fetch the folder id of the last breadcrumb.
      $id = key($breadcrumbs);

      // If current folder id doesn't match the folder id of last breadcrumb.
      if ($id != $current_folder->id && count($breadcrumbs) > 1) {

        // Remove the last breadcrumb since the user has navigated "Up"
        // at least 1 folder.
        array_pop($breadcrumbs);
      }

      // If the folder id of the last breadcrumb does not equal the current
      // folder id then keep removing breadcrumbs from the end.
    } while ($id != $current_folder->id && count($breadcrumbs) > 1);
  }

  // If the parent folder id of the current folder is in the breadcrumb trail
  // then the user MIGHT have navigated down into a subfolder.
  if (is_object($current_folder) && property_exists($current_folder, 'parent') && array_key_exists($current_folder->parent, $breadcrumbs)) {

    // Go to the end of the breadcrumb array.
    end($breadcrumbs);

    // If the last folder id in the breadcrumb equals the parent folder id of
    // the current folder the the user HAS navigated down into a subfolder.
    if (key($breadcrumbs) == $current_folder->parent) {

      // Add the current folder to the breadcrumb.
      $breadcrumbs[$current_folder->id] = $current_folder->name;
    }
  }

  // Reset the breadcrumb array so that it can be rendered in order.
  reset($breadcrumbs);

  // Create a container for the breadcrumb.
  $form['breadcrumb-container'] = [
    '#type' => 'container',
    // Custom element property to store breadcrumbs array.
    // This is fetched from the form state every time the form is rebuilt
    // due to navigating between folders.
    '#breadcrumbs' => $breadcrumbs,
    '#attributes' => [
      'class' => [
        'breadcrumb acquiadam-browser-breadcrumb-container',
      ],
    ],
  ];

  // Add the breadcrumb buttons to the form.
  foreach ($breadcrumbs as $folder_id => $folder_name) {
    $form['breadcrumb-container'][$folder_id] = [
      '#type' => 'button',
      '#value' => $folder_name,
      '#name' => 'acquiadam_folder',
      '#acquiadam_folder_id' => $folder_id,
      '#acquiadam_parent_folder_id' => $folder_name,
      '#prefix' => '<li>',
      '#suffix' => '</li>',
      '#attributes' => [
        'class' => [
          'acquiadam-browser-breadcrumb',
        ],
      ],
    ];
  }
  return $form;
}