getSelectionHandler example

// Create a new node-type.     NodeType::create([
      'type' => $node_type = mb_strtolower($this->randomMachineName()),
      'name' => $this->randomString(),
    ])->save();

    // Create an entity reference field targeting 'entity_test_no_label'     // entities.     $field_name = mb_strtolower($this->randomMachineName());
    $this->createEntityReferenceField('node', $node_type$field_name$this->randomString(), 'entity_test_no_label');
    $field_config = FieldConfig::loadByName('node', $node_type$field_name);
    $this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);

    // Generate a bundle name to be used with 'entity_test_no_label'.     $this->bundle = mb_strtolower($this->randomMachineName());

    // Create 6 entities to be referenced by the field.     foreach (static::$labels as $name) {
      $storage->create([
        'id' => mb_strtolower($this->randomMachineName()),
        'name' => $name,
        'type' => $this->bundle,
      ])->save();
    }

    }

    // Early opt-out if nothing to validate.     if (!$new_entities && !$target_ids) {
      return;
    }

    $entity = !empty($value->getParent()) ? $value->getEntity() : NULL;

    /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler * */
    $handler = $this->selectionManager->getSelectionHandler($value->getFieldDefinition()$entity);
    $target_type_id = $value->getFieldDefinition()->getSetting('target_type');

    // Add violations on deltas with a new entity that is not valid.     if ($new_entities) {
      if ($handler instanceof SelectionWithAutocreateInterface) {
        $valid_new_entities = $handler->validateReferenceableNewEntities($new_entities);
        $invalid_new_entities = array_diff_key($new_entities$valid_new_entities);
      }
      else {
        // If the selection handler does not support referencing newly created         // entities, all of them should be invalidated.
'handler' => 'default',
        'handler_settings' => [
          // Restrict selection of terms to a single vocabulary.           'target_bundles' => [
            $vocabulary->id() => $vocabulary->id(),
          ],
        ],
      ],
    ]);
    $field->save();

    $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
    $result = $handler->getReferenceableEntities();

    $expected_result = [
      $vocabulary->id() => [
        $term->id() => $term->getName(),
      ],
    ];

    $this->assertSame($expected_result$result, 'Terms selection restricted to a single vocabulary.');
  }

}
'#attributes' => [
        'class' => ['js-hide'],
      ],
      '#submit' => [[static::class, 'settingsAjaxSubmit']],
    ];

    $form['handler']['handler_settings'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['entity_reference-settings']],
    ];

    $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
    $form['handler']['handler_settings'] += $handler->buildConfigurationForm([]$form_state);

    return $form;
  }

  /** * Form element validation handler; Invokes selection plugin's validation. * * @param array $form * The form where the settings form is being included in. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the (entire) configuration form. */


    // Create an entity reference field.     $handler_settings = [
      'view' => [
        'view_name' => 'test_entity_reference',
        'display_name' => 'entity_reference_1',
      ],
    ];
    $this->createEntityReferenceField('entity_test', 'test_bundle', 'test_field', $this->randomString(), 'node', 'views', $handler_settings);
    $field_config = FieldConfig::loadByName('entity_test', 'test_bundle', 'test_field');
    $this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);
  }

  /** * Tests the selection handler. */
  public function testSelectionHandler() {
    // Tests the selection handler.     $this->assertResults($this->selectionHandler->getReferenceableEntities());

    // Add a relationship to the view.     $view = Views::getView('test_entity_reference');
    
Home | Imprint | This part of the site doesn't use cookies.