You are here

public function BusinessRulesViewsSelection::getReferenceableEntities in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/EntityReferenceSelection/BusinessRulesViewsSelection.php \Drupal\business_rules\Plugin\EntityReferenceSelection\BusinessRulesViewsSelection::getReferenceableEntities()

Gets the list of referenceable entities.

Return value

array A nested array of entities, the first level is keyed by the entity bundle, which contains an array of entity labels (escaped), keyed by the entity ID.

Overrides SelectionInterface::getReferenceableEntities

1 call to BusinessRulesViewsSelection::getReferenceableEntities()
BusinessRulesViewsSelection::countReferenceableEntities in src/Plugin/EntityReferenceSelection/BusinessRulesViewsSelection.php
Counts entities that are referenceable.

File

src/Plugin/EntityReferenceSelection/BusinessRulesViewsSelection.php, line 421

Class

BusinessRulesViewsSelection
Plugin override of the 'selection' entity_reference.

Namespace

Drupal\business_rules\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  $handler_settings = $this
    ->getHandlerSettings();
  $display_name = $handler_settings['business_rules_view']['display_name'];
  $arguments = $handler_settings['business_rules_view']['arguments'];
  $parent_field_value = $this
    ->getParentFieldValue($this->configuration['entity']);
  if (is_array($parent_field_value) && !empty($parent_field_value['target_id']) && preg_match('/\\((\\d+)\\)$/', $parent_field_value['target_id'], $matches)) {

    // If the field widget is entity autocomplete, the returned value is a
    // string which contains the entity id.
    $parent_field_value = $matches[1];
  }

  // If we have an array with values we should implode those values and enable
  // Allow multiple values into our contextual filter.
  if (is_array($parent_field_value)) {
    $parent_field_value = implode(",", $parent_field_value);
  }
  $arguments = !empty($parent_field_value) ? [
    $parent_field_value,
  ] + $arguments : $arguments;
  $result = [];
  if ($this
    ->initializeView($match, $match_operator, $limit)) {

    // Get the results.
    $result = $this->view
      ->executeDisplay($display_name, $arguments);
  }
  $return = [];
  if ($result) {
    foreach ($this->view->result as $row) {
      $entity = $row->_entity;
      $return[$entity
        ->bundle()][$entity
        ->id()] = $entity
        ->label();
    }
  }
  return $return;
}