You are here

public function ViewsReferenceTrait::fieldElement in Views Reference Field 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php \Drupal\viewsreference\Plugin\Field\FieldWidget\ViewsReferenceTrait::fieldElement()

Build the field element.

2 calls to ViewsReferenceTrait::fieldElement()
ViewsReferenceSelectWidget::formElement in src/Plugin/Field/FieldWidget/ViewsReferenceSelectWidget.php
Returns the form for a single field widget.
ViewsReferenceWidget::formElement in src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php
Returns the form for a single field widget.

File

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

Class

ViewsReferenceTrait
Trait for shared code in Viewsreference Field Widgets.

Namespace

Drupal\viewsreference\Plugin\Field\FieldWidget

Code

public function fieldElement($element, $items, $delta) {
  switch ($element['target_id']['#type']) {
    case 'select':
      $test = [
        '!value' => '_none',
      ];
      $event = 'change';
      break;
    default:
      $test = [
        'filled' => TRUE,
      ];
      $event = 'viewsreference-select';
      break;
  }
  $field_name = $items
    ->getName();
  $name = $field_name . '[' . $delta . '][target_id]';
  $element['target_id']['#target_type'] = 'view';
  $element['target_id']['#ajax'] = [
    'callback' => [
      $this,
      'getDisplayIds',
    ],
    'event' => $event,
    'progress' => [
      'type' => 'throbber',
      'message' => t('Getting display Ids...'),
    ],
  ];
  $default_value = isset($items[$delta]
    ->getValue()['display_id']) ? $items[$delta]
    ->getValue()['display_id'] : '';
  if ($default_value == '') {
    $options = $this
      ->getAllViewsDisplayIds();
  }
  else {
    $options = $this
      ->getViewDisplayIds($items[$delta]
      ->getValue()['target_id']);
  }

  // Build our target_id field name attribute from the parent elements.
  $field_name = $items
    ->getName();
  $field_path = !empty($element['target_id']['#field_parents']) ? $element['target_id']['#field_parents'] : [];
  $original_field_path = $field_path = array_merge($field_path, [
    $field_name,
    $delta,
    'target_id',
  ]);
  $name = array_shift($field_path);
  foreach ($field_path as $field_path_element) {
    $name .= '[' . $field_path_element . ']';
  }

  // We build a unique class name from field elements
  // And any parent elements that might exist
  // Which will be used to render the display id options in our ajax function.
  $class = implode('-', $original_field_path) . '-display';
  $element['display_id'] = [
    '#title' => 'Display Id',
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $default_value,
    '#weight' => 10,
    '#prefix' => '<span id="' . $class . '">',
    '#suffix' => '</span>',
    '#attributes' => [
      'class' => [
        'viewsreference-display-id',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="' . $name . '"]' => $test,
      ],
    ],
  ];
  $element['options'] = [
    '#type' => 'details',
    '#title' => t('Options'),
    '#weight' => 10,
  ];

  // Title and argument are the original options included in this module.
  $element['options']['title'] = [
    '#title' => 'Include View Title',
    '#type' => 'checkbox',
    '#default_value' => isset($items[$delta]
      ->getValue()['title']) ? $items[$delta]
      ->getValue()['title'] : '',
    '#weight' => 20,
    '#states' => [
      'visible' => [
        ':input[name="' . $name . '"]' => $test,
      ],
    ],
  ];
  $element['options']['argument'] = [
    '#title' => 'Argument',
    '#type' => 'textfield',
    '#default_value' => isset($items[$delta]
      ->getValue()['argument']) ? $items[$delta]
      ->getValue()['argument'] : '',
    '#weight' => 21,
    '#states' => [
      'visible' => [
        ':input[name="' . $name . '"]' => $test,
      ],
    ],
  ];
  $element['#attached']['library'][] = 'viewsreference/viewsreference';
  return $element;
}