You are here

public function ViewsReferenceTrait::getDisplayIds in Views Reference Field 8

AJAX function to get display IDs for a particular View.

File

src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php, line 133

Class

ViewsReferenceTrait
Trait for shared code in Viewsreference Field Widgets.

Namespace

Drupal\viewsreference\Plugin\Field\FieldWidget

Code

public function getDisplayIds(array &$form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();
  $field_value = NestedArray::getValue($form_state
    ->getValues(), $trigger['#parents']);

  // Get the value for the target id of the View.
  switch ($trigger['#type']) {
    case 'select':
      $view_id = $field_value[$trigger['#delta']][$trigger['#key_column']];
      break;
    default:
      $view_id = $field_value;
      break;
  }

  // Obtain the display ids for the given View.
  // We recreate the same unique class as in the parent function.
  $element_class_selector = '#' . implode('-', $trigger['#parents']) . '-display';

  // Change the display_id field options.
  $display_id_field_path = array_slice($trigger['#array_parents'], 0, -1);
  array_push($display_id_field_path, 'display_id');
  $display_id_field = NestedArray::getValue($form, $display_id_field_path);
  $display_id_field['#options'] = $this
    ->getViewDisplayIds($view_id);

  // Construct the html.
  $response = new AjaxResponse();
  $response
    ->addCommand(new ReplaceCommand($element_class_selector, render($display_id_field)));
  return $response;
}