You are here

function workbench_access_post_update_convert_to_scheme in Workbench Access 8

Convert configuration into a scheme.

File

./workbench_access.post_update.php, line 19
Contains post update hooks.

Code

function workbench_access_post_update_convert_to_scheme() {
  $config = \Drupal::state()
    ->get('workbench_access_original_configuration', FALSE);
  if (!$config) {
    throw new UpdateException('Did not find expected original configuration');
  }
  $settings = [];
  if ($config
    ->get('scheme') === 'taxonomy') {
    $fields = [];
    foreach ($config
      ->get('fields') as $entity_type => $field_info) {
      foreach (array_filter($field_info) as $bundle => $field_name) {
        $fields[] = [
          'entity_type' => $entity_type,
          'bundle' => $bundle,
          'field' => $field_name,
        ];
      }
    }
    $settings = [
      'vocabularies' => array_values($config
        ->get('parents')),
      'fields' => $fields,
    ];
  }
  elseif ($config
    ->get('scheme') === 'menu') {
    $settings = [
      'menus' => $config
        ->get('parents'),
      'bundles' => array_keys($config
        ->get('fields')['node']),
    ];
  }

  // Let other modules intervene for additional types.
  \Drupal::moduleHandler()
    ->alter('workbench_access_scheme_update', $settings, $config);

  // No settings? Do nothing but mark the update.
  if (empty($settings)) {
    $message = t('Workbench Access has not been configured. Disabling the module is recommended.');
  }
  else {
    $scheme = AccessScheme::create([
      'id' => 'default',
      'label' => $config
        ->get('label'),
      'plural_label' => $config
        ->get('plural_label'),
      'scheme' => $config
        ->get('scheme'),
      'scheme_settings' => $settings,
    ]);
    $scheme
      ->save();
  }
  \Drupal::state()
    ->set('workbench_access_upgraded_scheme_id', 'default');

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  foreach (\Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->loadMultiple() as $node_type) {
    $node_type
      ->unsetThirdPartySetting('workbench_access', 'workbench_access_status');
    $node_type
      ->save();
  }
  if (isset($message)) {
    return $message;
  }
}