Commit 148dbcba authored by Pedro Cambra's avatar Pedro Cambra Committed by Pedro Cambra
Browse files

Issue #3305464 by pcambra: This entity (type: id) cannot be referenced when...

Issue #3305464 by pcambra: This entity (type: id) cannot be referenced when disabling the parent field
parent d4d7cd68
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\dependent_fields\Plugin\EntityReferenceSelection;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\dependent_fields\Ajax\UpdateOptionsCommand;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
@@ -520,7 +521,14 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    $display_name = $this->configuration['dependent_fields_view']['display_name'];
    $arguments = $this->configuration['dependent_fields_view']['arguments'];
    $parent_field_value = $this->getParentFieldValue($this->configuration['entity']);
    $entity = $this->configuration['entity'];

    if ($entity instanceof FieldableEntityInterface) {
      $parent_field_value = $this->getParentFieldValue($entity);
    }
    else {
      $parent_field_value = $this->getParentFieldValue();
    }
    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.
@@ -564,12 +572,21 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
  public function validateReferenceableEntities(array $ids) {
    $display_name = $this->configuration['dependent_fields_view']['display_name'];
    $arguments = $this->configuration['dependent_fields_view']['arguments'];
    $entity = $this->configuration['entity'];

    if ($entity instanceof FieldableEntityInterface) {
      $parent_field_value = $this->getParentFieldValue($entity);
    }
    else {
      $parent_field_value = $this->getParentFieldValue();
    }

    // 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 = [];
    $ids = $this->getValidIds($parent_field_value);
@@ -596,7 +613,7 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
    $arguments = $this->configuration['dependent_fields_view']['arguments'];
    $arguments = !empty($parent_field_value) ? [$parent_field_value] + $arguments : $arguments;
    $result = [];
    if ($this->initializeView(NULL, 'CONTAINS', 0)) {
    if ($this->initializeView()) {
      // Get the results.
      $result = $this->view->executeDisplay($display_name, $arguments);
    }