/** * Handles the response for inline entity form autocompletion. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function autocomplete($entity_type_id, $field_name, $bundle, Request $request) { $string = $request->query->get('q'); $fields = $this->entityManager->getFieldDefinitions($entity_type_id, $bundle); $widget = $this->entityManager->getStorage('entity_form_display')->load($entity_type_id . '.' . $bundle . '.default')->getComponent($field_name); // The path was passed invalid parameters, or the string is empty. // strlen() is used instead of empty() since '0' is a valid value. if (!isset($fields[$field_name]) || !$widget || !strlen($string)) { throw new AccessDeniedHttpException(); } $field = $fields[$field_name]; $results = array(); if ($field->getType() == 'entity_reference') { /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler */ $handler = $this->selectionManager->getSelectionHandler($field); $entity_labels = $handler->getReferenceableEntities($string, $widget['settings']['match_operator'], 10); foreach ($entity_labels as $bundle => $labels) { // Loop through each entity type, and autocomplete with its titles. foreach ($labels as $entity_id => $label) { // entityreference has already check_plain-ed the title. $results[] = t('!label (!entity_id)', array('!label' => $label, '!entity_id' => $entity_id)); } } } $matches = array(); foreach ($results as $result) { // Strip things like starting/trailing white spaces, line breaks and tags. $key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($result))))); $matches[] = ['value' => $key, 'label' => '<div class="reference-autocomplete">' . $result . '</div>']; } return new JsonResponse($matches); }
/** * {@inheritdoc} */ public function identitiesCanRegister($entity_type, array $entity_ids) { if (in_array($entity_type, $this->getIdentityTypes())) { if ($this->duplicateRegistrantsAllowed()) { return $entity_ids; } else { $options = ['target_type' => $entity_type, 'handler' => 'rng_register', 'handler_settings' => ['event_entity_type' => $this->getEvent()->getEntityTypeId(), 'event_entity_id' => $this->getEvent()->id()]]; /* @var $selection \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface */ $selection = $this->selectionPluginManager->getInstance($options); return $selection->validateReferenceableEntities($entity_ids); } } return []; }
/** * Returns matched labels based on a given search string. * * @param string $target_type * The ID of the target entity type. * @param string $selection_handler * The plugin ID of the entity reference selection handler. * @param array $selection_settings * An array of settings that will be passed to the selection handler. * @param string $string * (optional) The label of the entity to query by. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * Thrown when the current user doesn't have access to the specifies entity. * * @return array * An array of matched entity labels, in the format required by the AJAX * autocomplete API (e.g. array('value' => $value, 'label' => $label)). * * @see \Drupal\system\Controller\EntityAutocompleteController */ public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') { $matches = array(); $options = array('target_type' => $target_type, 'handler' => $selection_handler, 'handler_settings' => $selection_settings); $handler = $this->selectionManager->getInstance($options); if (isset($string)) { // Get an array of matching entities. $match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS'; $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10); // Loop through the entities and convert them into autocomplete output. foreach ($entity_labels as $values) { foreach ($values as $entity_id => $label) { $key = "{$label} ({$entity_id})"; // Strip things like starting/trailing white spaces, line breaks and // tags. $key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(String::decodeEntities(strip_tags($key))))); // Names containing commas or quotes must be wrapped in quotes. $key = Tags::encode($key); $matches[] = array('value' => $key, 'label' => $label); } } } return $matches; }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { /** @var \Drupal\Core\Field\FieldItemListInterface $value */ /** @var ValidReferenceConstraint $constraint */ if (!isset($value)) { return; } // Collect new entities and IDs of existing entities across the field items. $new_entities = []; $target_ids = []; foreach ($value as $delta => $item) { $target_id = $item->target_id; // We don't use a regular NotNull constraint for the target_id property as // NULL is allowed if the entity property contains an unsaved entity. // @see \Drupal\Core\TypedData\DataReferenceTargetDefinition::getConstraints() if (!$item->isEmpty() && $target_id === NULL) { if (!$item->entity->isNew()) { $this->context->buildViolation($constraint->nullMessage)->atPath((string) $delta)->addViolation(); return; } $new_entities[$delta] = $item->entity; } // '0' or NULL are considered valid empty references. if (!empty($target_id)) { $target_ids[$delta] = $target_id; } } // Early opt-out if nothing to validate. if (!$new_entities && !$target_ids) { return; } /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler * */ $handler = $this->selectionManager->getSelectionHandler($value->getFieldDefinition()); $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. $invalid_new_entities = $new_entities; } foreach ($invalid_new_entities as $delta => $entity) { $this->context->buildViolation($constraint->invalidAutocreateMessage)->setParameter('%type', $target_type_id)->setParameter('%label', $entity->label())->atPath((string) $delta . '.entity')->setInvalidValue($entity)->addViolation(); } } // Add violations on deltas with a target_id that is not valid. if ($target_ids) { $valid_target_ids = $handler->validateReferenceableEntities($target_ids); if ($invalid_target_ids = array_diff($target_ids, $valid_target_ids)) { // For accuracy of the error message, differentiate non-referenceable // and non-existent entities. $target_type = $this->entityTypeManager->getDefinition($target_type_id); $existing_ids = $this->entityTypeManager->getStorage($target_type_id)->getQuery()->condition($target_type->getKey('id'), $invalid_target_ids, 'IN')->execute(); foreach ($invalid_target_ids as $delta => $target_id) { $message = in_array($target_id, $existing_ids) ? $constraint->message : $constraint->nonExistingMessage; $this->context->buildViolation($message)->setParameter('%type', $target_type_id)->setParameter('%id', $target_id)->atPath((string) $delta . '.target_id')->setInvalidValue($target_id)->addViolation(); } } } }