You are here

public static function EntityconnectFormUtils::getReferenceFields in Entity connect 8.2

Extracts all reference fields from the given form.

Parameters

array $form: The form to extract fields from.

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

Return value

array The array of reference fields extracted.

2 calls to EntityconnectFormUtils::getReferenceFields()
EntityconnectFormUtils::entityFormAlter in src/EntityconnectFormUtils.php
Add the entityconnect button(s) to the form.
EntityconnectFormUtils::validateForm in src/EntityconnectFormUtils.php
Form API #validate callback for a form with entity_reference fields.

File

src/EntityconnectFormUtils.php, line 120

Class

EntityconnectFormUtils
Contains form alter, callbacks and utility methods for entityconnect.

Namespace

Drupal\entityconnect

Code

public static function getReferenceFields(array &$form, FormStateInterface $form_state) {
  $ref_fields = [];

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = NULL;

  // Get the entity if this is an entity form.
  if (method_exists($form_state
    ->getFormObject(), 'getEntity')) {
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
  }

  // Bail out if not a fieldable entity form.
  if (empty($entity) || !$entity
    ->getEntityType()
    ->entityClassImplements('\\Drupal\\Core\\Entity\\FieldableEntityInterface')) {
    return $ref_fields;
  }

  // Get the entity reference elements from this form.
  $field_defs = $entity
    ->getFieldDefinitions();
  foreach (Element::getVisibleChildren($form) as $child) {
    if (!isset($field_defs[$child])) {
      continue;
    }
    $field_definition = $field_defs[$child];
    if ($field_definition
      ->getType() == 'entity_reference') {

      // Fields must be configurable.
      if ($field_definition instanceof FieldConfig) {
        $ref_fields[] = $child;
      }
    }
  }
  \Drupal::moduleHandler()
    ->alter('entityconnect_ref_fields', $ref_fields);
  return $ref_fields;
}