You are here

workbench_moderation_actions.install in Workbench Moderation Actions 8

File

workbench_moderation_actions.install
View source
<?php

/**
 * @file
 * Contains workbench_moderation.install.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\system\Entity\Action;
use Drupal\workbench_moderation_actions\Plugin\Deriver\StateChangeDeriver;

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

  // Remove the publish/unpublished actions.
  if ($action = Action::load('node_publish_action')) {
    $action
      ->delete();
  }
  if ($action = Action::load('node_unpublish_action')) {
    $action
      ->delete();
  }
  $deriver = StateChangeDeriver::create(\Drupal::getContainer(), 'state_change');
  foreach ($deriver
    ->getDerivativeDefinitions([]) as $key => $definition) {
    $plugin_id = 'state_change:' . $key;
    Action::create([
      'id' => str_replace(':', '__', $plugin_id),
      'label' => $definition['label'],
      'type' => $definition['type'],
      'plugin' => $plugin_id,
      'configuration' => [],
    ])
      ->save();
  }
}

/**
 * Implements hook_uninstall().
 */
function workbench_moderation_actions_uninstall() {

  // Restore the publish/unpublished actions.
  $node_actions_to_restore = [
    'node_publish_action',
    'node_unpublish_action',
  ];
  foreach ($node_actions_to_restore as $action) {
    if (!Action::load($action)) {
      $entity_type_manager = \Drupal::entityTypeManager();
      $module_handler = \Drupal::moduleHandler();

      // Restore the action from the original config from the node module.
      $config_install_path = $module_handler
        ->getModule('node')
        ->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
      $storage = new FileStorage($config_install_path);
      $entity_type_manager
        ->getStorage('action')
        ->create($storage
        ->read('system.action.' . $action))
        ->trustData()
        ->save();
    }
  }
}