You are here

function wbm2cm_modules_installed in Workbench Moderation to Content Moderation 8.2

Implements hook_modules_installed().

1 call to wbm2cm_modules_installed()
WorkflowCollectorTest::testWorkflowCollection in tests/src/Kernel/WorkflowCollectorTest.php

File

./wbm2cm.module, line 11

Code

function wbm2cm_modules_installed(array $modules) {
  if (in_array('content_moderation', $modules)) {
    $state = \Drupal::state();
    $permission_map = [];
    $all_states = [
      'all' => 'all',
    ];
    foreach ($state
      ->get('moderation_workflows', []) as $workflow) {
      foreach (array_keys($workflow['type_settings']['states']) as $state_id) {
        array_push($all_states, $workflow['id'] . "-{$state_id}");
      }

      /** @var \Drupal\workflows\WorkflowInterface $workflow */
      $workflow = Workflow::create($workflow);

      // Set a flag so that entity hooks can tell if this workflow is being
      // created by wbm2cm. Normally this would be a bad practice, but there are
      // extenuating circumstances.
      $workflow->wbm2cm = TRUE;
      $workflow
        ->save();
      foreach ($workflow
        ->getTypePlugin()
        ->getTransitions() as $id => $transition) {
        $old_permission = "use {$id} transition";
        $permission_map[$old_permission][] = 'use ' . $workflow
          ->id() . " transition {$id}";
      }
    }

    // Restore all moderation state filters to views.
    foreach ($state
      ->get('moderation_filters', []) as $filter) {
      list($view_id, $display_id, $filter_id) = $filter;
      $config = \Drupal::configFactory()
        ->getEditable($view_id);
      $key = "display.{$display_id}.display_options.filters.{$filter_id}";
      $filter = $config
        ->get($key);
      $filter['operator'] = 'in';
      $filter['value'] = array_combine($all_states, $all_states);
      $filter['expose']['reduce'] = FALSE;
      unset($filter['entity_field']);
      $filter['plugin_id'] = 'moderation_state_filter';
      $config
        ->set($key, $filter)
        ->save();
    }

    // Restore all moderation state fields to views.
    foreach ($state
      ->get('moderation_fields', []) as $field) {
      list($view_id, $display_id, $field_id) = $field;
      $root_key = "display.{$display_id}.display_options";
      $config = \Drupal::configFactory()
        ->getEditable($view_id);
      $key = "{$root_key}.fields.{$field_id}";
      $field = $original_field = $config
        ->get($key);
      $field['table'] = \Drupal::entityTypeManager()
        ->getDefinition('content_moderation_state')
        ->getDataTable();
      $field['click_sort_column'] = 'value';
      $field['type'] = 'string';
      $field['settings']['link_to_entity'] = FALSE;
      unset($field['settings']['link']);
      $field['group_column'] = 'value';
      $field['entity_type'] = 'content_moderation_state';
      $config
        ->set($key, $field)
        ->save();
    }

    /** @var \Drupal\user\RoleInterface $role */
    foreach (Role::loadMultiple() as $role) {
      $changed = FALSE;
      foreach ($permission_map as $revoke => $grants) {
        if ($role
          ->hasPermission($revoke)) {
          $changed = TRUE;
          $role
            ->revokePermission($revoke);
          array_walk($grants, [
            $role,
            'grantPermission',
          ]);
        }
      }
      if ($changed) {
        $role
          ->save();
      }
    }
  }
}