You are here

protected function EntityconnectWidgetProcessor::attachButtons in Entity connect 8.2

Attach the entity connect buttons to a single widget element.

Parameters

array $element: The widget element to attach the button to.

string $key: The key of an autocomplete widget element.

File

src/EntityconnectWidgetProcessor.php, line 143

Class

EntityconnectWidgetProcessor
A reference field widget processing class for entityconnect module.

Namespace

Drupal\entityconnect

Code

protected function attachButtons(array &$element, $key = 'all') {

  // Get the parents.
  $parents = '';
  if (isset($this->widget['#field_parents'])) {
    foreach ($this->widget['#field_parents'] as $parent) {
      $parents .= ($parents ? '-' : '') . $parent;
    }
  }
  $fieldStorage = $this->fieldDefinition
    ->getFieldStorageDefinition();
  $extraClass = isset($this->widget['#type']) ? $this->widget['#type'] : 'autocomplete';
  $extraClass .= $fieldStorage
    ->getCardinality() > 1 ? ' multiple-values' : ' single-value';
  $extraClass .= isset($this->widget['#multiple']) && $this->widget['#multiple'] == TRUE ? ' multiple-selection' : ' single-selection';
  if (isset($this->widget['#type'])) {
    if (isset($this->widget['#multiple']) && $this->widget['#multiple'] == TRUE || $this->widget['#type'] == 'radios' || $this->widget['#type'] == 'checkboxes') {
      $element['#attributes']['class'][] = 'inline-label';
    }
  }

  // Set the class strings for the button.
  $buttonClasses = [
    'extra_class' => $extraClass,
    'parents_class' => $parents,
  ];

  // Set the correct element to attach to.
  if ($key === 'all') {

    // Options widget.
    if (isset($this->widget['#type'])) {
      $widgetElement =& $element;
    }
    else {
      $widgetElement =& $element['widget'];
    }
  }
  else {
    $widgetElement =& $element['widget'][$key];
  }
  $this
    ->attachAddButton($widgetElement, $buttonClasses, $key);
  $this
    ->attachEditButton($widgetElement, $buttonClasses, $key);
}