flag_lists-4.0.x-dev/modules/flag_lists_actions/flag_lists_actions.module
modules/flag_lists_actions/flag_lists_actions.module
<?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\Entity\FlaggingCollection;
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['actions']['submit']['#submit'][] = '::submitForm';
}
}
/**
* 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');
}
/**
* Prepare for saving the Action.
*
* @param \Drupal\flag_lists\Entity\FlaggingCollection $entity
* The entity to create an Action for.
*/
function flag_lists_actions_prepare(FlaggingCollection $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);
}
}
