You are here

flag_lists_actions.module in Flag Lists 4.0.x

File

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

/**
 * @file
 * Contains flag_lists_actions.module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\flag_lists_actions\Controller\FlagListsActionsController;

/**
 * Implements hook_form_alter().
 */
function flag_lists_actions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $flagListsService = \Drupal::service('flaglists');
  $config = \Drupal::config('flag_lists.settings');

  // Change form id here.
  if (in_array($form_id, [
    'flag_lists_settings_form',
  ])) {
    $actionOverviewLink = Link::createFromRoute('Action Overview', 'entity.action.collection')
      ->toString();
    $form['flag_settings']['hide_actions'] = [
      '#type' => 'checkbox',
      '#title' => t('Hide Flagging Collection Actions in the @link.', [
        '@link' => $actionOverviewLink,
      ]),
      '#default_value' => $config
        ->get('hide_actions'),
      '#description' => t('Do you want to hide the Flagging Collections Actions in the @link?', [
        '@link' => $actionOverviewLink,
      ]),
    ];
    $form['actions']['submit']['#submit'][] = 'flag_lists_actions_save_submit';
  }
}

/**
 * Form submission handler for the 'save' action.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 */
function flag_lists_actions_save_submit(array &$form, FormStateInterface $form_state) {
  \Drupal::configFactory()
    ->getEditable('flag_lists.settings')
    ->set('hide_actions', $form_state
    ->getValue('hide_actions'))
    ->save();
}

/**
 * Implements hook_entity_type_build().
 */
function flag_lists_actions_entity_type_build(array &$entity_types) {

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['action']
    ->setListBuilderClass('Drupal\\flag_lists_actions\\FlagListsActionsActionListBuilder');
}

/**
 * Implements hook_ENTITY_presave().
 */
function flag_lists_actions_flagging_collection_presave(EntityInterface $entity) {
  $flag = $entity
    ->getRelatedFlag();
  if ($flag
    ->isSyncing()) {

    // Do not create actions when config is progress of synchronization.
    return;
  }

  // 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();
  FlagListsActionsController::createActions($entity);
}

/**
 * Implements hook_ENTITY_delete().
 */
function flag_lists_actions_flagging_collection_delete(EntityInterface $entity) {
  $flag = $entity
    ->getRelatedFlag();

  // Do not delete actions when config is progress of synchronization.
  if ($flag) {
    if ($flag
      ->isSyncing()) {
      return;
    }
    FlagListsActionsController::deleteActions($entity);
  }
}

Functions

Namesort descending Description
flag_lists_actions_entity_type_build Implements hook_entity_type_build().
flag_lists_actions_flagging_collection_delete Implements hook_ENTITY_delete().
flag_lists_actions_flagging_collection_presave Implements hook_ENTITY_presave().
flag_lists_actions_form_alter Implements hook_form_alter().
flag_lists_actions_save_submit Form submission handler for the 'save' action.