You are here

public function EntityReference::statesHandler in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 src/Plugin/conditional_fields/handler/EntityReference.php \Drupal\conditional_fields\Plugin\conditional_fields\handler\EntityReference::statesHandler()

Executes states handler according to conditional fields settings.

Overrides ConditionalFieldsHandlersPluginInterface::statesHandler

File

src/Plugin/conditional_fields/handler/EntityReference.php, line 21

Class

EntityReference
Provides states handler for entity reference fields.

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler

Code

public function statesHandler($field, $field_info, $options) {
  $state = [];
  $values_set = $options['values_set'];
  switch ($values_set) {
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
      $value_form = $this
        ->getWidgetValue($options['value_form']);
      if (empty($value_form)) {
        break;
      }
      if ($options['field_cardinality'] == 1) {
        $node = Node::load($value_form[0]['target_id']);
        if ($node instanceof Node) {

          // Create an array of valid formats of title for autocomplete.
          $state[$options['state']][$options['selector']] = $this
            ->getAutocompleteSuggestions($node);
        }
      }
      else {
        $ids = array_column($value_form, 'target_id');
        $nodes = Node::loadMultiple($ids);
        if (!empty($nodes)) {
          foreach (array_values($nodes) as $key => $node) {
            $selector = str_replace('[0]', "[{$key}]", $options['selector']);
            $state[$options['state']][$selector] = $this
              ->getAutocompleteSuggestions($node);
          }
        }
      }
      break;
    default:
      break;
  }
  return $state;
}