You are here

public function DomainElementManager::setFormOptions in Domain Access 8

Resets form options and stores hidden values that the user cannot change.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

string $field_name: The name of the field to check.

bool $hide_on_disallow: If the field is set to a value that cannot be altered by the user who is not assigned to that domain, pass TRUE to remove the form element entirely. See DomainSourceElementManager for the use-case.

Return value

array Return the modified form array.

Overrides DomainElementManagerInterface::setFormOptions

File

domain/src/DomainElementManager.php, line 55

Class

DomainElementManager
Generic base class for handling hidden field options.

Namespace

Drupal\domain

Code

public function setFormOptions(array $form, FormStateInterface $form_state, $field_name, $hide_on_disallow = FALSE) {

  // There are cases, such as Entity Browser, where the form is partially
  // invoked, but without our fields.
  if (!isset($form[$field_name])) {
    return $form;
  }
  $fields = $this
    ->fieldList($field_name);
  $empty = FALSE;
  $disallowed = $this
    ->disallowedOptions($form_state, $form[$field_name]);
  if (empty($form[$field_name]['widget']['#options']) || count($form[$field_name]['widget']['#options']) == 1 && isset($form[$field_name]['widget']['#options']['_none'])) {
    $empty = TRUE;
  }

  // If the domain form element is set as a group, and the field is not
  // assigned to another group, then move it. See
  // domain_access_form_node_form_alter().
  if (isset($form['domain']) && !isset($form[$field_name]['#group'])) {
    $form[$field_name]['#group'] = 'domain';
  }

  // If no values and we should hide the element, do so.
  if ($hide_on_disallow && $empty) {
    $form[$field_name]['#access'] = FALSE;
  }

  // Check for domains the user cannot access or the absence of any options.
  if (!empty($disallowed) || $empty) {

    // @TODO: Potentially show this information to users with permission.
    $form[$field_name . '_disallowed'] = [
      '#type' => 'value',
      '#value' => $disallowed,
    ];
    $form['domain_hidden_fields'] = [
      '#type' => 'value',
      '#value' => $fields,
    ];
    if ($hide_on_disallow || $empty) {
      $form[$field_name]['#access'] = FALSE;
    }
    elseif (!empty($disallowed)) {
      $form[$field_name]['widget']['#description'] .= $this
        ->listDisallowed($disallowed);
    }

    // Call our submit function to merge in values.
    // Account for all the submit buttons on the node form.
    $buttons = [
      'preview',
      'delete',
    ];
    $submit = $this
      ->getSubmitHandler();
    foreach ($form['actions'] as $key => $action) {
      if (!in_array($key, $buttons) && isset($form['actions'][$key]['#submit']) && !in_array($submit, $form['actions'][$key]['#submit'])) {
        array_unshift($form['actions'][$key]['#submit'], $submit);
      }
    }
  }
  return $form;
}