You are here

public function Taxonomy::buildConfigurationForm in Workbench Access 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides AccessControlHierarchyBase::buildConfigurationForm

File

src/Plugin/AccessControlHierarchy/Taxonomy.php, line 344

Class

Taxonomy
Defines a hierarchy based on a Vocabulary.

Namespace

Drupal\workbench_access\Plugin\AccessControlHierarchy

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form['vocabularies'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Vocabularies'),
    '#description' => $this
      ->t('Select the vocabularies to use for access control'),
    '#default_value' => $this->configuration['vocabularies'],
    '#options' => array_map(function (VocabularyInterface $vocabulary) {
      return $vocabulary
        ->label();
    }, $this->entityTypeManager
      ->getStorage('taxonomy_vocabulary')
      ->loadMultiple()),
  ];
  $entity_reference_fields = $this->entityFieldManager
    ->getFieldMapByFieldType('entity_reference');
  $taxonomy_fields = [];
  foreach ($entity_reference_fields as $entity_type_id => $fields) {
    foreach ($fields as $field_name => $details) {

      // Parent fields on taxonomy terms would create infinite loops. Deny.
      if ($entity_type_id == 'taxonomy_term' && $field_name == 'parent') {
        continue;
      }
      foreach ($details['bundles'] as $bundle) {
        $field_definitions = $this->entityFieldManager
          ->getFieldDefinitions($entity_type_id, $bundle);
        if (isset($field_definitions[$field_name]) && $field_definitions[$field_name]
          ->getFieldStorageDefinition()
          ->getSetting('target_type') === 'taxonomy_term') {
          $handler_settings = $field_definitions[$field_name]
            ->getSetting('handler_settings');

          // Must refer to a proper target. Target bundles referring to
          // themselves would create an infinite loop. Deny.
          if ($entity_type_id == 'taxonomy_term' && in_array($bundle, $this->configuration['vocabularies'], TRUE)) {
            continue;
          }

          // Must have a proper target.
          if (!isset($handler_settings['target_bundles'])) {
            continue;
          }

          // At least one target must be configured for access control.
          $allowed = array_intersect($handler_settings['target_bundles'], $this->configuration['vocabularies']);
          if (empty($allowed)) {
            continue;
          }

          // Create a unique key for each option.
          $key = sprintf('%s:%s:%s', $entity_type_id, $bundle, $field_name);
          $taxonomy_fields[$key] = [
            'entity_type' => $this->entityTypeManager
              ->getDefinition($entity_type_id)
              ->getLabel(),
            'bundle' => $this->bundleInfo
              ->getBundleInfo($entity_type_id)[$bundle]['label'],
            'field' => $field_definitions[$field_name]
              ->getLabel(),
          ];
          $validate[$key] = $handler_settings['target_bundles'];
        }
      }
    }
  }
  if (!$taxonomy_fields) {
    $form['fields'] = [
      '#markup' => $this
        ->t('There are no configured taxonomy fields, please create a new term reference field on a content type to continue'),
    ];
    return $form;
  }
  $default_value = array_map(function (array $field) {
    $field += [
      'entity_type' => NULL,
      'bundle' => NULL,
      'field' => '',
    ];
    return sprintf('%s:%s:%s', $field['entity_type'], $field['bundle'], $field['field']);
  }, $this->configuration['fields']);
  $form['fields'] = [
    '#type' => 'tableselect',
    '#header' => [
      'entity_type' => $this
        ->t('Entity type'),
      'bundle' => $this
        ->t('Bundle'),
      'field' => $this
        ->t('Field name'),
    ],
    '#options' => $taxonomy_fields,
    '#default_value' => array_combine($default_value, $default_value),
  ];
  $form['validate'] = [
    '#type' => 'value',
    '#value' => $validate,
  ];
  return $form;
}