diff --git a/entity_reference_patterns.module b/entity_reference_patterns.module index 56cf03b..3d66a4d 100644 --- a/entity_reference_patterns.module +++ b/entity_reference_patterns.module @@ -1,12 +1,19 @@ getSelectionCriteria() as $type => $condition) { if ($type == 'bundles') { foreach ($condition as $key => $val) { - $row['conditions']['#items'][] = $this->t('The %type bundle is %bundle', ['%type' => $entity->getType(), '%bundle' => $key]); + $row['conditions']['#items'][] = $this->t('The %type bundle is %bundle', [ + '%type' => $entity->getType(), + ' + %bundle' => $key, + ]); } } } @@ -68,7 +68,7 @@ class EntityReferencePatternListBuilder extends DraggableListBuilder { 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 530]), ]; - if ($entity->access('duplicate') ) { + if ($entity->access('duplicate')) { $operations['duplicate'] = [ 'title' => $this->t('Duplicate'), 'weight' => 0, @@ -76,7 +76,7 @@ class EntityReferencePatternListBuilder extends DraggableListBuilder { 'attributes' => $ajax_attributes, ]; } - if ($entity->access('update') ) { + if ($entity->access('update')) { $operations['edit'] = [ 'title' => $this->t('Edit'), 'weight' => 0, @@ -84,7 +84,7 @@ class EntityReferencePatternListBuilder extends DraggableListBuilder { 'attributes' => $ajax_attributes, ]; } - if ($entity->access('delete') ) { + if ($entity->access('delete')) { $operations['delete'] = [ 'title' => $this->t('Delete'), 'weight' => 0, diff --git a/src/Element/EntityAutocomplete.php b/src/Element/EntityAutocomplete.php index 90633eb..0e1ebf5 100644 --- a/src/Element/EntityAutocomplete.php +++ b/src/Element/EntityAutocomplete.php @@ -7,7 +7,7 @@ use Drupal\Component\Utility\Tags; use Drupal\entity_reference_patterns\Entity\EntityReferencePatternEntity; /** - * Extends core FormElement("entity_autocomplete") to support custom default value labels. + * Support custom default value labels. */ class EntityAutocomplete extends EntityAutocompleteCore { @@ -65,7 +65,8 @@ class EntityAutocomplete extends EntityAutocompleteCore { $label = ($entity->access('view label')) ? $entity->label() : t('- Restricted access -'); } - // Just to be safe here we replace ([digital]) petterns so referenced ID could be extracted. + // Just to be safe here we replace ([digital]) petterns so + // referenced ID could be extracted. $label = preg_replace("/\(([\d\h,]+)\)/", "[$1]", $label); // Take into account "autocreated" entities. diff --git a/src/Entity/EntityReferencePatternEntity.php b/src/Entity/EntityReferencePatternEntity.php index 362fd43..42cbe37 100755 --- a/src/Entity/EntityReferencePatternEntity.php +++ b/src/Entity/EntityReferencePatternEntity.php @@ -100,6 +100,8 @@ class EntityReferencePatternEntity extends ConfigEntityBase implements EntityRef protected $selection_criteria = []; /** + * Store Weight. + * * @var int */ protected $weight = 0; diff --git a/src/Entity/Form/PatternEditForm.php b/src/Entity/Form/PatternEditForm.php index 9b57bfe..3f1b026 100644 --- a/src/Entity/Form/PatternEditForm.php +++ b/src/Entity/Form/PatternEditForm.php @@ -17,6 +17,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class PatternEditForm extends EntityForm { /** + * Store Entity Object. + * * @var \Drupal\pathauto\PathautoPatternInterface */ protected $entity; @@ -179,7 +181,7 @@ class PatternEditForm extends EntityForm { $form['actions']['cancel'] = [ '#type' => 'link', '#title' => $this->t('Cancel'), - '#url' => Url::fromRoute('entity.entity_reference_pattern.collection'), + '#url' => Url::fromRoute('entity.entity_reference_pattern.collection'), '#weight' => 10, '#attributes' => ['class' => ['button', 'dialog-cancel']], ]; @@ -199,7 +201,7 @@ class PatternEditForm extends EntityForm { } $entity->setSelectionCriteria([ - 'bundles' => !empty($form_state->getValue('bundles')) ? array_filter($form_state->getValue('bundles')) : [] + 'bundles' => !empty($form_state->getValue('bundles')) ? array_filter($form_state->getValue('bundles')) : [], ]); return $entity; diff --git a/src/EntityReferencePatternMatcher.php b/src/EntityReferencePatternMatcher.php index eca54aa..cbbd816 100644 --- a/src/EntityReferencePatternMatcher.php +++ b/src/EntityReferencePatternMatcher.php @@ -6,14 +6,69 @@ use Drupal\Core\Entity\EntityAutocompleteMatcher; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Tags; use Drupal\entity_reference_patterns\Entity\EntityReferencePatternEntity; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Utility\Token; +use Drupal\Core\Entity\EntityTypeManagerInterface; +/** + * Autocomplete Result for Enity Refrence. + */ class EntityReferencePatternMatcher extends EntityAutocompleteMatcher { + /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + + /** + * The token replacement service. + * + * @var \Drupal\Core\Utility\Token + */ + protected $token; + + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * Constructs an MediaFilterController instance. + * + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. + * @param \Drupal\Core\Utility\Token $token + * The token replacement service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager service. + */ + public function __construct(EntityRepositoryInterface $entity_repository, Token $token = NULL, EntityTypeManagerInterface $entity_type_manager) { + $this->entityRepository = $entity_repository; + $this->token = $token; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.repository'), + $container->get('token'), + $container->get('entity_type.manager') + ); + } + /** * Gets matched labels based on a given search string. */ public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') { - $entity_ids = \Drupal::entityQuery('entity_reference_pattern') + $entity_ids = $this->entityTypeManager->getStorage('entity_reference_pattern')->getQuery() ->condition('type', $target_type) ->condition('status', 1) ->sort('weight', 'DESC') @@ -32,7 +87,6 @@ class EntityReferencePatternMatcher extends EntityAutocompleteMatcher { ]; $handler = $this->selectionManager->getInstance($options); - $token = \Drupal::token(); if (!empty($string)) { // Get an array of matching entities. @@ -42,29 +96,31 @@ class EntityReferencePatternMatcher extends EntityAutocompleteMatcher { // 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::service('entity.repository')->getTranslationFromContext($entity); + $entity = $this->entityTypeManager->getStorage($target_type)->load($entity_id); + $entity = $this->entityRepository->getTranslationFromContext($entity); if (!empty($criteria['bundles'])) { foreach ($criteria as $type => $condition) { if ($type == 'bundles') { foreach ($condition as $k => $v) { if ($k == $entity->bundle()) { - $label = $token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity]); + $label = $this->token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity]); } } } } } else { - $label = $token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity]); + $label = $this->token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity]); } - // Just to be safe here we replace ([digital]) petterns so referenced ID could be extracted. + // Just to be safe here we replace ([digital]) petterns so + // referenced ID could be extracted. $label = preg_replace("/\(([\d\h,]+)\)/", "[$1]", $label); $key = $label . ' (' . $entity_id . ')'; - // Strip things like starting/trailing white spaces, line breaks and tags. + // 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); diff --git a/src/EntityReferencePatterns.php b/src/EntityReferencePatterns.php index e5f7ae7..bda73d5 100644 --- a/src/EntityReferencePatterns.php +++ b/src/EntityReferencePatterns.php @@ -4,6 +4,10 @@ namespace Drupal\entity_reference_patterns; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\entity_reference_patterns\Entity\EntityReferencePatternEntity; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Utility\Token; +use Drupal\Core\Entity\EntityTypeManagerInterface; /** * Entity Reference Patterns module helper. @@ -14,10 +18,58 @@ class EntityReferencePatterns { use StringTranslationTrait; + /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + + /** + * The token replacement service. + * + * @var \Drupal\Core\Utility\Token + */ + protected $token; + + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * Constructs an MediaFilterController instance. + * + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. + * @param \Drupal\Core\Utility\Token $token + * The token replacement service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager service. + */ + public function __construct(EntityRepositoryInterface $entity_repository, Token $token = NULL, EntityTypeManagerInterface $entity_type_manager) { + $this->entityRepository = $entity_repository; + $this->token = $token; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.repository'), + $container->get('token'), + $container->get('entity_type.manager') + ); + } + /** * Implements hook_options_list_alter(). */ - public function hook_options_list_alter(&$options, $context) { + public function hookOptionsListAlter(&$options, $context) { $fieldDefinition = $context['fieldDefinition']->getFieldStorageDefinition(); $settings = $fieldDefinition->getSettings(); $new_options = []; @@ -26,10 +78,10 @@ class EntityReferencePatterns { foreach ($options as $bundle_label => $items) { if ($bundle_label !== '_none' && is_array($items)) { foreach ($items as $entity_id => $label) { - $entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($entity_id); + $entity = $this->entityTypeManager->getStorage($target_type)->load($entity_id); // Set the entity in the correct language for display. - $entity = $entity_repository->getTranslationFromContext($entity); - $entity_ids = \Drupal::entityQuery('entity_reference_pattern') + $entity = $this->entityRepository->getTranslationFromContext($entity); + $entity_ids = $this->entityTypeManager->getStorage('entity_reference_pattern')->getQuery() ->condition('type', $entity->getEntityType()->id()) ->condition('status', 1) ->sort('weight', 'DESC') @@ -44,16 +96,16 @@ class EntityReferencePatterns { if ($type == 'bundles') { foreach ($condition as $k => $v) { if ($k == $entity->bundle()) { - $new_label = $token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity], ['clear' => TRUE]); + $new_label = $this->token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity], ['clear' => TRUE]); } } } } } else { - $new_label = $token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity], ['clear' => TRUE]); + $new_label = $this->token->replace($label_config_entity->getPattern(), [$entity->getEntityType()->id() => $entity], ['clear' => TRUE]); } - $new_options[$bundle_label][$entity_id] = isset($new_label) ? $new_label : $label; + $new_options[$bundle_label][$entity_id] = $new_label ?? $label; } else { $new_options[$bundle_label][$entity_id] = $label; diff --git a/src/Routing/EntityReferencePatternRouteSubscriber.php b/src/Routing/EntityReferencePatternRouteSubscriber.php index 88adea4..2528df6 100644 --- a/src/Routing/EntityReferencePatternRouteSubscriber.php +++ b/src/Routing/EntityReferencePatternRouteSubscriber.php @@ -5,8 +5,14 @@ namespace Drupal\entity_reference_patterns\Routing; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Component\Routing\RouteCollection; +/** + * Used To Alter Routes. + */ class EntityReferencePatternRouteSubscriber extends RouteSubscriberBase { + /** + * Alter Existing Routes. + */ public function alterRoutes(RouteCollection $collection) { if ($route = $collection->get('system.entity_autocomplete')) { $route->setDefault('_controller', '\Drupal\entity_reference_patterns\Controller\EntityAutocompleteController::handleAutocomplete');