You are here

public function ScheduledTransitionsSettingsForm::buildForm in Scheduled Transitions 2.x

Same name and namespace in other branches
  1. 8 src/Form/ScheduledTransitionsSettingsForm.php \Drupal\scheduled_transitions\Form\ScheduledTransitionsSettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/ScheduledTransitionsSettingsForm.php, line 110

Class

ScheduledTransitionsSettingsForm
Settings for scheduled transitions.

Namespace

Drupal\scheduled_transitions\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) : array {
  $form['bundles'] = [
    '#type' => 'details',
  ];
  $form['bundles']['help'] = [
    '#prefix' => '<p>',
    '#markup' => $this
      ->t('Enable entity type/bundles for use with scheduled transitions.'),
    '#suffix' => '</p>',
  ];
  $form['bundles']['enabled'] = [
    '#type' => 'tableselect',
    '#header' => [
      $this
        ->t('Entity type'),
      $this
        ->t('Bundle'),
      $this
        ->t('Permissions'),
    ],
  ];

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple();
  $enabledBundles = [];
  foreach ($this->scheduledTransitionsUtility
    ->getBundles() as $entityTypeId => $bundles) {
    foreach ($bundles as $bundle) {
      $enabledBundles[] = sprintf('%s:%s', $entityTypeId, $bundle);
    }
  }

  // Flattens possible entity type/bundles.
  $possibleBundles = [];
  foreach ($this->scheduledTransitionsUtility
    ->getApplicableBundles() as $entityTypeId => $bundles) {
    foreach ($bundles as $bundle) {
      $possibleBundles[] = sprintf('%s:%s', $entityTypeId, $bundle);
    }
  }
  $form['bundles']['enabled']['#options'] = array_map(function (string $bundle) use ($roles, $enabledBundles) : array {
    $checked = in_array($bundle, $enabledBundles, TRUE);
    [
      $entityTypeId,
      $bundle,
    ] = explode(':', $bundle);
    $entityType = $this->entityTypeManager
      ->getDefinition($entityTypeId);
    $bundleInfo = $this->bundleInfo
      ->getBundleInfo($entityTypeId);

    // Show a handy permissions message, but only if this bundle is enabled.
    if ($checked) {
      $permissionMessage = $this
        ->t('<strong>Notice</strong>: no roles are currently granted permissions for this type.');
      $needsPermissions = [
        Permissions::viewScheduledTransitionsPermission($entityTypeId, $bundle),
        Permissions::addScheduledTransitionsPermission($entityTypeId, $bundle),
      ];

      // Find at least one role with at least one of the permissions.
      foreach ($roles as $role) {
        if (count(array_intersect($needsPermissions, $role
          ->getPermissions()))) {
          $permissionMessage = '';
          break;
        }
      }
    }
    else {
      $permissionMessage = '';
    }
    return [
      [
        'data' => [
          '#markup' => $entityType ? $entityType
            ->getLabel() : '',
        ],
      ],
      [
        'data' => [
          '#markup' => $bundleInfo[$bundle]['label'] ?? '',
        ],
      ],
      [
        'data' => [
          '#markup' => $permissionMessage,
        ],
      ],
    ];
  }, array_combine($possibleBundles, $possibleBundles));

  // Collapse the details element if anything is enabled.
  $form['bundles']['#title'] = $this
    ->t('Enabled types (@count)', [
    '@count' => count($enabledBundles),
  ]);
  $form['bundles']['#open'] = count($enabledBundles) === 0;
  $form['bundles']['enabled']['#default_value'] = array_fill_keys($enabledBundles, TRUE);
  $settings = $this
    ->config('scheduled_transitions.settings');
  $form['cron'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Automation'),
    '#open' => TRUE,
  ];
  $form['cron']['cron_create_queue_items'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create scheduling jobs in cron'),
    '#description' => $this
      ->t('If this setting is not enabled, items must be created using <code>drush scheduled-transitions:queue-jobs</code> command.'),
    '#default_value' => (bool) $settings
      ->get('automation.cron_create_queue_items'),
  ];
  $form['entity_operations'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Entity operations'),
    '#open' => TRUE,
  ];
  $form['entity_operations']['mirror_operation_view'] = [
    '#type' => 'select',
    '#title' => 'Mirror view scheduled transitions',
    '#description' => $this
      ->t('When attempting to <em>view scheduled transitions</em> for an entity, defer access to another operation.'),
    '#field_suffix' => $this
      ->t('operation'),
    '#options' => [
      'update' => $this
        ->t('Update'),
    ],
    '#empty_option' => $this
      ->t('- Disabled -'),
    '#default_value' => $settings
      ->get('mirror_operations.view scheduled transition'),
  ];
  $form['entity_operations']['mirror_operation_add'] = [
    '#type' => 'select',
    '#title' => 'Mirror add scheduled transitions',
    '#description' => $this
      ->t('When attempting to <em>add scheduled transitions</em> for an entity, defer access to another operation.'),
    '#field_suffix' => $this
      ->t('operation'),
    '#options' => [
      'update' => $this
        ->t('Update'),
    ],
    '#empty_option' => $this
      ->t('- Disabled -'),
    '#default_value' => $settings
      ->get('mirror_operations.add scheduled transition'),
  ];
  $form['entity_operations']['mirror_operation_reschedule'] = [
    '#type' => 'select',
    '#title' => 'Mirror reschedule scheduled transitions',
    '#description' => $this
      ->t('When attempting to <em>reschedule scheduled transitions</em> for an entity, defer access to another operation.'),
    '#field_suffix' => $this
      ->t('operation'),
    '#options' => [
      'update' => $this
        ->t('Update'),
    ],
    '#empty_option' => $this
      ->t('- Disable -'),
    '#default_value' => $settings
      ->get('mirror_operations.reschedule scheduled transitions'),
  ];
  $form['messages'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Messages'),
    '#open' => TRUE,
  ];
  $form['message_override'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Revision log override'),
    '#group' => 'messages',
    '#default_value' => $settings
      ->get('message_override'),
    '#description' => $this
      ->t('Whether users are permitted to override revision log templates per scheduled transition.'),
  ];
  $form['message_transition_latest'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Latest'),
    '#group' => 'messages',
    '#default_value' => $settings
      ->get('message_transition_latest'),
    '#description' => $this
      ->t('Available tokens: [scheduled-transitions:from-revision-id] [scheduled-transitions:from-state] [scheduled-transitions:to-state]'),
  ];
  $form['message_transition_historical'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Historical'),
    '#group' => 'messages',
    '#default_value' => $settings
      ->get('message_transition_historical'),
    '#description' => $this
      ->t('Available tokens: [scheduled-transitions:from-revision-id] [scheduled-transitions:from-state] [scheduled-transitions:to-state]'),
  ];
  $form['message_transition_copy_latest_draft'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Copy latest draft'),
    '#group' => 'messages',
    '#default_value' => $settings
      ->get('message_transition_copy_latest_draft'),
    '#description' => $this
      ->t('Available tokens: [scheduled-transitions:latest-state] [scheduled-transitions:latest-revision-id]'),
  ];
  return parent::buildForm($form, $form_state);
}