You are here

flag_lists_actions.install in Flag Lists 4.0.x

Contains install and updates for the flag_lists_actions.

File

modules/flag_lists_actions/flag_lists_actions.install
View source
<?php

/**
 * @file
 * Contains install and updates for the flag_lists_actions.
 */
use Drupal\flag_lists_actions\Controller\FlagListsActionsController;

/**
 * Implements hook_install().
 */
function flag_lists_actions_install() {

  // Add Flagging Collection Actions for all Flagging Collection.
  $flagListsService = \Drupal::service('flaglists');
  $count = 0;

  // The action plugin cache needs to detect the new flag.

  /** @var \Drupal\Core\Action\ActionManager $action_manager */
  $action_manager = \Drupal::service('plugin.manager.action');
  $action_manager
    ->clearCachedDefinitions();
  foreach ($flagListsService
    ->getAllFlaggingCollections() as $collection) {
    $flag = $collection
      ->getRelatedFlag();
    if ($flag
      ->isSyncing()) {

      // Do not create actions when config is progress of synchronization.
      return;
    }
    FlagListsActionsController::createActions($collection);
    $count = $count + 1;
  }
  $message = t('Created Actions for @count existing Flagging Collections.', [
    '@count' => $count,
  ]);
  $messenger = \Drupal::messenger();
  $logger = \Drupal::logger('flag_lists');
  $messenger
    ->addStatus($message);
  $logger
    ->info($message);
}

/**
 * Implements hook_uninstall().
 */
function flag_lists_actions_uninstall($is_syncing) {

  // Remove all Flagging Collection Actions.
  $flagListsService = \Drupal::service('flaglists');
  $count = 0;
  foreach ($flagListsService
    ->getAllFlaggingCollections() as $collection) {
    $flag = $collection
      ->getRelatedFlag();

    // Do not delete actions when config is progress of synchronization.
    if ($flag) {
      if ($flag
        ->isSyncing()) {
        return;
      }
      FlagListsActionsController::deleteActions($collection);
      $count = $count + 1;
    }
  }
  $message = t('Deleted Actions for @count Flagging Collections.', [
    '@count' => $count,
  ]);
  $messenger = \Drupal::messenger();
  $logger = \Drupal::logger('flag_lists');
  $messenger
    ->addStatus($message);
  $logger
    ->info($message);
}

Functions