You are here

public function Taxonomy::alterForm in Workbench Access 8

Alters the selection options provided for an access control field.

Parameters

\Drupal\workbench_access\Entity\AccessSchemeInterface $scheme: Access scheme.

array $form: The content entry form to alter.

\Drupal\Core\Form\FormStateInterface $form_state: Active form state data.

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity object that the form is modifying.

Overrides AccessControlHierarchyBase::alterForm

File

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

Class

Taxonomy
Defines a hierarchy based on a Vocabulary.

Namespace

Drupal\workbench_access\Plugin\AccessControlHierarchy

Code

public function alterForm(AccessSchemeInterface $scheme, array &$form, FormStateInterface &$form_state, ContentEntityInterface $entity) {
  foreach (array_column($this
    ->getApplicableFields($entity
    ->getEntityTypeId(), $entity
    ->bundle()), 'field') as $field) {
    if (!isset($form[$field])) {
      continue;
    }
    $element =& $form[$field];
    if (isset($element['widget']['#options'])) {
      foreach ($element['widget']['#options'] as $id => $data) {

        // When using a select list, options may be a nested array.
        if (is_array($data)) {
          foreach ($data as $index => $item) {
            $sections = [
              $index,
            ];
            if (empty(WorkbenchAccessManager::checkTree($scheme, $sections, $this->userSectionStorage
              ->getUserSections($scheme)))) {
              unset($element['widget']['#options'][$id][$index]);
            }
          }

          // If the parent is empty, remove it.
          if (empty($element['widget']['#options'][$id])) {
            unset($element['widget']['#options'][$id]);
          }
        }
        else {
          $sections = [
            $id,
          ];
          if ($id !== '_none' && empty(WorkbenchAccessManager::checkTree($scheme, $sections, $this->userSectionStorage
            ->getUserSections($scheme)))) {
            unset($element['widget']['#options'][$id]);
          }
        }
      }
    }
    else {
      foreach ($element['widget'] as $key => $item) {
        if (is_array($item) && isset($item['target_id']['#type']) && $item['target_id']['#type'] == 'entity_autocomplete') {
          $element['widget'][$key]['target_id']['#selection_handler'] = 'workbench_access:taxonomy_term:' . $scheme
            ->id();
          $element['widget'][$key]['target_id']['#validate_reference'] = TRUE;

          // Hide elements that cannot be edited.
          if (!empty($element['widget'][$key]['target_id']['#default_value'])) {
            $sections = [
              $element['widget'][$key]['target_id']['#default_value']
                ->id(),
            ];
            if (empty(WorkbenchAccessManager::checkTree($scheme, $sections, $this->userSectionStorage
              ->getUserSections($scheme)))) {
              unset($element['widget'][$key]);
              $id = current($sections);
              $disallowed[$id] = $id;
            }
          }
        }
      }
    }
    if (!empty($disallowed)) {
      $form['workbench_access_disallowed']['#tree'] = TRUE;
      $form['workbench_access_disallowed'][$field] = [
        $scheme
          ->id() => [
          '#type' => 'value',
          '#value' => $disallowed,
        ],
      ];
    }
  }
}