ContextMapper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Drupal\ctools;
  3. use Drupal\Core\Entity\EntityRepositoryInterface;
  4. use Drupal\Core\Plugin\Context\Context;
  5. use Drupal\Core\Plugin\Context\ContextDefinition;
  6. use Drupal\ctools\Context\EntityLazyLoadContext;
  7. /**
  8. * Maps context configurations to context objects.
  9. */
  10. class ContextMapper implements ContextMapperInterface {
  11. /**
  12. * The entity repository.
  13. *
  14. * @var \Drupal\Core\Entity\EntityRepositoryInterface
  15. */
  16. protected $entityRepository;
  17. /**
  18. * Constructs a new ContextMapper.
  19. *
  20. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
  21. * The entity repository.
  22. */
  23. public function __construct(EntityRepositoryInterface $entity_repository) {
  24. $this->entityRepository = $entity_repository;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getContextValues(array $context_configurations) {
  30. $contexts = [];
  31. foreach ($context_configurations as $name => $context_configuration) {
  32. $context_definition = new ContextDefinition($context_configuration['type'], $context_configuration['label'], TRUE, FALSE, $context_configuration['description']);
  33. if (strpos($context_configuration['type'], 'entity:') === 0) {
  34. $context = new EntityLazyLoadContext($context_definition, $this->entityRepository, $context_configuration['value']);
  35. }
  36. else {
  37. $context = new Context($context_definition, $context_configuration['value']);
  38. }
  39. $contexts[$name] = $context;
  40. }
  41. return $contexts;
  42. }
  43. }