You are here

public static function EntityconnectWidgetProcessor::process in Entity connect 8.2

Form API callback: Processes an entity_reference field element.

Adds entityconnect buttons to the field.

This method is assigned as a #process callback in entityconnect_form_alter() function.

Parameters

array $element: The widget container element to attach the buttons.

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

array $form: The parent entity form.

Return value

array The altered element.

File

src/EntityconnectWidgetProcessor.php, line 93

Class

EntityconnectWidgetProcessor
A reference field widget processing class for entityconnect module.

Namespace

Drupal\entityconnect

Code

public static function process(array $element, FormStateInterface $form_state, array $form) {
  if (!method_exists($form_state
    ->getFormObject(), 'getEntity')) {
    return $element;
  }
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $fieldDefinition = $entity
    ->getFieldDefinition($element['widget']['#field_name']);

  // Instantiate this class so we don't have to pass variables around.
  $widgetProcessor = new EntityconnectWidgetProcessor($fieldDefinition, $element['widget']);

  // Give other contrib modules the chance to change the target.
  $entityType = $widgetProcessor
    ->getEntityType();
  $acceptableTypes = $widgetProcessor
    ->getAcceptableTypes();
  $data = [
    'entity_type' => &$entityType,
    'acceptable_types' => &$acceptableTypes,
    'field' => $fieldDefinition,
  ];
  \Drupal::moduleHandler()
    ->alter('entityconnect_field_attach_form', $data);
  $widgetProcessor
    ->setEntityType($data['entity_type']);
  $widgetProcessor
    ->setAcceptableTypes($data['acceptable_types']);

  // We currently should separate Autocomplete widget from others
  // because "Edit" button will not react well on multiple selected items.
  // Autocomplete widget has no #type value, so we are testing it
  // via $element['widget']['#type']. This does not apply to
  // Autocomplete Tags widget so we have to check for target_id too.
  if (isset($element['widget']['#type']) || isset($element['widget']['target_id'])) {
    $widgetProcessor
      ->attachButtons($element);
  }
  else {
    foreach (Element::getVisibleChildren($element['widget']) as $key) {
      if (is_numeric($key)) {
        $widgetProcessor
          ->attachButtons($element, $key);
      }
    }
  }
  return $element;
}