You are here

build_hooks.module in Build Hooks 3.x

Same filename and directory in other branches
  1. 8.2 build_hooks.module
  2. 8 build_hooks.module

Contains build_hooks.module.

File

build_hooks.module
View source
<?php

/**
 * @file
 * Contains build_hooks.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function build_hooks_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the build_hooks module.
    case 'help.page.build_hooks':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Build Hooks') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_cron().
 */
function build_hooks_cron() {

  // Deploy all environments marked for cron deployment:
  build_hooks_get_trigger_service()
    ->deployFrontendCronEnvironments();
}

/**
 * Implements hook_entity_update().
 */
function build_hooks_entity_update(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    if (!$entity
      ->isNew() && build_hooks_get_logger_service()
      ->isEntityTypeLoggable($entity)) {
      $triggerService = build_hooks_get_trigger_service();
      build_hooks_get_logger_service()
        ->logEntityUpdated($entity);
      $triggerService
        ->deployFrontendEntityUpdateEnvironments();
      $triggerService
        ->invalidateToolbarCacheTag();
    }
  }
}

/**
 * Implements hook_entity_insert().
 */
function build_hooks_entity_insert(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    if (build_hooks_get_logger_service()
      ->isEntityTypeLoggable($entity)) {
      $triggerService = build_hooks_get_trigger_service();
      build_hooks_get_logger_service()
        ->logEntityCreated($entity);
      $triggerService
        ->deployFrontendEntityUpdateEnvironments();
      $triggerService
        ->invalidateToolbarCacheTag();
    }
  }
}

/**
 * Implements hook_entity_delete().
 */
function build_hooks_entity_delete(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    if (build_hooks_get_logger_service()
      ->isEntityTypeLoggable($entity)) {
      $triggerService = build_hooks_get_trigger_service();
      build_hooks_get_logger_service()
        ->logEntityDeleted($entity);
      $triggerService
        ->deployFrontendEntityUpdateEnvironments();
      $triggerService
        ->invalidateToolbarCacheTag();
    }
  }
}

/**
 * Implements hook_toolbar().
 */
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;
}

/**
 * Implements hook_toolbar_alter().
 */
function build_hooks_toolbar_alter(&$items) {

  // Not that great, but it works:
  // We add our own cache tag to the home element of the toolbar
  // which we can be sure is always there.
  $items['home']['#cache'] = [
    'tags' => [
      'config:frontend_environment_list',
      build_hooks_get_trigger_service()
        ->getToolbarCacheTag(),
    ],
  ];
}

/**
 * Get the trigger service.
 *
 * @return \Drupal\build_hooks\Trigger
 *   The trigger service.
 */
function build_hooks_get_trigger_service() {
  return \Drupal::service('build_hooks.trigger');
}

/**
 * Get the deploy logger service.
 *
 * @return \Drupal\build_hooks\DeployLogger
 *   The deploy logger service.
 */
function build_hooks_get_logger_service() {
  return \Drupal::service('build_hooks.deploylogger');
}