-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #656 from chapter-three/deployment/20220216
Deployment/20220216
- Loading branch information
Showing
10 changed files
with
230 additions
and
1,780 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
web/modules/custom/jcc_autocomplete_helper/jcc_autocomplete_helper.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
web/modules/custom/jcc_autocomplete_helper/jcc_autocomplete_helper.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
104 changes: 104 additions & 0 deletions
104
...modules/custom/jcc_autocomplete_helper/src/Controller/JccEntityAutocompleteController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
web/modules/custom/jcc_autocomplete_helper/src/JccEntityAutocompleteMatcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
web/modules/custom/jcc_autocomplete_helper/src/Routing/AutocompleteRouteSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.