You are here

protected function EntityconnectWidgetProcessor::initTargetInfo in Entity connect 8.2

Initialize entityType and targetBundles from the handler settings.

1 call to EntityconnectWidgetProcessor::initTargetInfo()
EntityconnectWidgetProcessor::__construct in src/EntityconnectWidgetProcessor.php
Constructs a EntityconnectWidgetProcessor object.

File

src/EntityconnectWidgetProcessor.php, line 381

Class

EntityconnectWidgetProcessor
A reference field widget processing class for entityconnect module.

Namespace

Drupal\entityconnect

Code

protected function initTargetInfo() {
  $targetSettings = $this->fieldDefinition
    ->getSettings();
  $this->entityType = $targetSettings['target_type'];
  $this->acceptableTypes = [];

  // If this is the default setting then just get the target bundles.
  if (isset($targetSettings['handler_settings']['target_bundles'])) {
    if (!is_null($targetSettings['handler_settings']['target_bundles'])) {
      $this->acceptableTypes = $targetSettings['handler_settings']['target_bundles'];
    }
    else {

      // Use the entity type if the target entity has no bundles.
      $this->acceptableTypes[] = $this->entityType;
    }
  }
  elseif ($targetSettings['handler'] == 'views') {
    $view = Views::getView($targetSettings['handler_settings']['view']['view_name']);

    // Get filters from the entity_reference display.
    $viewDisplay = $view->storage
      ->getDisplay($targetSettings['handler_settings']['view']['display_name']);
    if (!isset($viewDisplay['display_options']['filters'])) {

      // Get filters from the Master display.
      $viewDisplay = $view->storage
        ->getDisplay('default');
    }
    switch ($this->entityType) {

      // Type(bundle) value is under vid key for taxonomy terms.
      case 'taxonomy_term':
        if (isset($viewDisplay['display_options']['filters']['vid'])) {
          $this->acceptableTypes = $viewDisplay['display_options']['filters']['vid']['value'];
        }
        break;

      // Otherwise, type(bundle) value is under type key.
      default:
        if (isset($viewDisplay['display_options']['filters']['type'])) {
          $this->acceptableTypes = $viewDisplay['display_options']['filters']['type']['value'];
        }

        // $this->acceptableTypes was already set to empty array before.
        break;
    }
  }
}