Skip to content

Commit

Permalink
Merge pull request #656 from chapter-three/deployment/20220216
Browse files Browse the repository at this point in the history
Deployment/20220216
  • Loading branch information
melwong-jcc authored Feb 16, 2022
2 parents 1e763f5 + eb051d3 commit 92f4259
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 1,780 deletions.
1 change: 1 addition & 0 deletions config/config-default/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module:
image: 0
inline_entity_form: 0
inline_form_errors: 0
jcc_autocomplete_helper: 0
jcc_boilerplate: 0
jcc_chatbot: 0
jcc_datefinder: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: JCC Autocomplete Helper
description: Adds the node's case type to autocomplete entities when available.
type: module
package: Custom
core: 8.x
core_version_requirement: ^8 || ^9
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
jcc_autocomplete_helper.route_subscriber:
class: Drupal\jcc_autocomplete_helper\Routing\AutocompleteRouteSubscriber
tags:
- { name: event_subscriber }

jcc_autocomplete_helper.autocomplete_matcher:
class: Drupal\jcc_autocomplete_helper\JccEntityAutocompleteMatcher
arguments: ['@plugin.manager.entity_reference_selection']
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Drupal\jcc_autocomplete_helper\Controller;

use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Tags;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\jcc_autocomplete_helper\JccEntityAutocompleteMatcher;
use Drupal\Component\Utility\Unicode;



class JccEntityAutocompleteController extends \Drupal\system\Controller\EntityAutocompleteController {

/**
* The autocomplete matcher for entity references.
*/
protected $matcher;

/**
* The key value store.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $keyValue;

/**
* {@inheritdoc}
*/
public function __construct(JccEntityAutocompleteMatcher $matcher, KeyValueStoreInterface $key_value) {
$this->matcher = $matcher;
$this->keyValue = $key_value;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('jcc_autocomplete_helper.autocomplete_matcher'),
$container->get('keyvalue')->get('entity_autocomplete')
);
}

/**
* Autocomplete the label of an entity.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object that contains the typed tags.
* @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 string $selection_settings_key
* The hashed key of the key/value entry that holds the selection handler
* settings.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The matched entity labels as a JSON response.
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* Thrown if the selection settings key is not found in the key/value store
* or if it does not match the stored data.
*/
public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
$matches = array();

// Get the typed string from the URL, if it exists.
if ($input = $request->query
->get('q')) {
$typed_string = Tags::explode($input);
$typed_string = Unicode::strtolower(array_pop($typed_string));

// Selection settings are passed in as a hashed key of a serialized array
// stored in the key/value store.
$selection_settings = $this->keyValue
->get($selection_settings_key, FALSE);
if ($selection_settings !== FALSE) {
$selection_settings_hash = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt());
if ($selection_settings_hash !== $selection_settings_key) {

// Disallow access when the selection settings hash does not match the
// passed-in key.
throw new AccessDeniedHttpException('Invalid selection settings key.');
}
}
else {

// Disallow access when the selection settings key is not found in the
// key/value store.
throw new AccessDeniedHttpException();
}
$matches = $this->matcher
->getMatches($target_type, $selection_handler, $selection_settings, $typed_string);
}

return new JsonResponse($matches);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Drupal\jcc_autocomplete_helper;

use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\EntityAutocompleteMatcher;
use Drupal\taxonomy\Entity\Term;

/**
* Autocomplete spotlight search customizations.
*/
class JccEntityAutocompleteMatcher extends EntityAutocompleteMatcher {

/**
* Gets matched labels based on a given search string.
*/
public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') {
$matches = [];

$options = [
'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, 50);

// Loop through the entities and convert them into autocomplete output.
foreach ($entity_labels as $values) {
foreach ($values as $entity_id => $label) {
$entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($entity_id);
$entity = \Drupal::entityManager()->getTranslationFromContext($entity);

$key = "{$label} ({$entity_id})";

// Strip things like starting/trailing white spaces, line breaks and
// tags.
$key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key)))));
// Names containing commas or quotes must be wrapped in quotes.
$key = Tags::encode($key);

// If node is tagged newslink, prefix title with type.
if ($entity->getEntityType()->id() == 'node') {
if($entity->hasField('field_case_types')) {
$case_type = $entity->get('field_case_types')->target_id;

if ($case_type != null) {
$case_type_label = Term::load($case_type)->getName();
$label = '(' . $case_type_label . ') ' . $label;
}
}
}

$matches[] = [
'value' => $key,
'label' => $label,
];
}
}
}

return $matches;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Drupal\jcc_autocomplete_helper\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

class AutocompleteRouteSubscriber extends RouteSubscriberBase {

public function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.entity_autocomplete')){
$route->setDefault('_controller', '\Drupal\jcc_autocomplete_helper\Controller\JccEntityAutocompleteController::handleAutocomplete');
}
}

}
Loading

0 comments on commit 92f4259

Please sign in to comment.