You are here

public function ViewfieldWidgetSelect::ajaxGetDisplayOptions in Viewfield 8.3

Ajax callback to retrieve display IDs.

Parameters

array $form: The form from which the display IDs are being requested.

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

Return value

\Drupal\Core\Ajax\AjaxResponse The Ajax response.

File

src/Plugin/Field/FieldWidget/ViewfieldWidgetSelect.php, line 270

Class

ViewfieldWidgetSelect
Plugin implementation of the 'viewfield_select' widget.

Namespace

Drupal\viewfield\Plugin\Field\FieldWidget

Code

public function ajaxGetDisplayOptions(array &$form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();
  $form_state_keys = array_slice($trigger['#parents'], 0, -1);
  $form_state_value = $form_state
    ->getValue($form_state_keys);
  $display_options = $trigger['#field_item']
    ->getDisplayOptions($form_state_value['target_id']);
  $html = '';
  foreach ($display_options as $key => $value) {
    $html .= '<option value="' . $key . '">' . $value . '</option>';
  }
  $html = '<optgroup>' . $html . '</optgroup>';

  // Create a class selector for Ajax response.
  $selector = '.' . $this
    ->createDisplayClass($form_state_keys);
  $response = new AjaxResponse();
  $response
    ->addCommand(new HtmlCommand($selector, $html));
  $response
    ->addCommand(new InvokeCommand($selector, 'ajaxEnableElements'));
  $form_state
    ->setRebuild(TRUE);
  return $response;
}