You are here

function build_hooks_toolbar in Build Hooks 3.x

Same name and namespace in other branches
  1. 8.2 build_hooks.module \build_hooks_toolbar()
  2. 8 build_hooks.module \build_hooks_toolbar()

Implements hook_toolbar().

File

./build_hooks.module, line 82
Contains build_hooks.module.

Code

function build_hooks_toolbar() {
  $triggerService = build_hooks_get_trigger_service();
  $items = [];
  if ($triggerService
    ->showMenu()) {

    /** @var \Drupal\Core\Entity\EntityTypeManager $entityTypeManager */
    $entityTypeManager = \Drupal::service('entity_type.manager');

    // Load all defined environments.
    $allEnvironments = $entityTypeManager
      ->getStorage('frontend_environment')
      ->loadByProperties([
      'status' => TRUE,
    ]);
    $has_envs = !empty($allEnvironments);
    $cache = [
      'tags' => [
        $triggerService
          ->getToolbarCacheTag(),
      ],
      'contexts' => [
        'user.permissions',
      ],
    ];
    if ($has_envs && count($allEnvironments) < 2) {
      foreach ($allEnvironments as $environment) {
        $num = build_hooks_get_logger_service()
          ->getNumberOfItemsSinceLastDeploymentForEnvironment($environment);

        /** @var \Drupal\build_hooks\Entity\FrontendEnvironment $environment */
        $items['build_hooks-' . $environment
          ->id()] = [
          '#cache' => $cache,
          '#weight' => $environment
            ->getWeight() + 999,
          '#type' => 'toolbar_item',
          'tab' => [
            '#type' => 'link',
            '#title' => \Drupal::translation()
              ->formatPlural($num, '@envName (1 change)', '@envName (@count changes)', [
              '@envName' => $environment
                ->label(),
            ]),
            '#url' => Url::fromRoute('build_hooks.deployment_form', [
              'frontend_environment' => $environment
                ->id(),
            ]),
          ],
        ];
      }
    }
    elseif ($has_envs) {
      $bh_items = [];
      $totalChanges = 0;
      foreach ($allEnvironments as $environment) {
        $changes = build_hooks_get_logger_service()
          ->getNumberOfItemsSinceLastDeploymentForEnvironment($environment);
        $totalChanges += $changes;

        /** @var \Drupal\build_hooks\Entity\FrontendEnvironment $environment */
        $bh_items['build_hooks_' . $environment
          ->id()] = [
          '#type' => 'link',
          '#title' => \Drupal::translation()
            ->formatPlural($changes, '@envName (1 change)', '@envName (@count changes)', [
            '@envName' => $environment
              ->label(),
          ]),
          '#url' => Url::fromRoute('build_hooks.deployment_form', [
            'frontend_environment' => $environment
              ->id(),
          ]),
        ];
      }
      $items['build_hooks_deployments'] = [
        '#cache' => $cache,
        '#type' => 'toolbar_item',
        'tab' => [
          '#type' => 'link',
          '#title' => \Drupal::translation()
            ->formatPlural($totalChanges, 'Deployments (1 total change)', 'Deployments (@count total changes)'),
          // @todo: Create a separate page for links to all environments?
          '#url' => Url::fromRoute('build_hooks.admin_config_build_hooks'),
        ],
        'tray' => [
          'deployments' => [
            '#theme' => 'item_list',
            '#wrapper_attributes' => [
              'class' => 'build-hooks-deployments',
            ],
            '#attributes' => [
              'class' => 'toolbar-menu',
            ],
            '#items' => $bh_items,
          ],
        ],
        '#weight' => 150,
      ];
    }
  }
  return $items;
}