Skip to content
Snippets Groups Projects

PHPMD pass

Files
12
@@ -64,6 +64,13 @@ abstract class ComponentFormatterBase extends FormatterBase {
*/
protected $routeMatch;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The provided plugin contexts.
*
@@ -96,6 +103,7 @@ abstract class ComponentFormatterBase extends FormatterBase {
$instance->sampleEntityGenerator = $container->get('ui_patterns.sample_entity_generator');
$instance->entityFieldManager = $container->get('entity_field.manager');
$instance->routeMatch = $container->get('current_route_match');
$instance->moduleHandler = $container->get('module_handler');
return $instance;
}
@@ -211,48 +219,14 @@ abstract class ComponentFormatterBase extends FormatterBase {
$contexts = $this->context ?? [];
$field_definition = $this->fieldDefinition;
$entity_type_id = $field_definition->getTargetEntityTypeId();
$entity = NULL;
// When field items are available, we can get the entity directly.
if ($items) {
$entity = $items->getEntity();
$entity = ($items) ? $items->getEntity() : NULL;
if (!$entity && ($layoutBuilderContext = $this->getLayoutBuilderEntityContext()) && $layoutBuilderContext["entity"]) {
$contexts = array_merge($contexts, $layoutBuilderContext["context"]);
$entity = $layoutBuilderContext["entity"];
}
if (!$entity) {
// Infer the entity context, when in layout builder.
$section_storage = $this->routeMatch->getParameter("section_storage");
if ($section_storage instanceof SectionStorageInterface) {
$section_storage_contexts = $section_storage->getContexts();
$contexts = array_merge($contexts, $section_storage_contexts);
if (!array_key_exists("entity", $contexts)) {
// Search for the entity in the context, with another name.
foreach ($section_storage_contexts as $section_storage_context) {
if ($section_storage_context instanceof EntityContext) {
// Now check if it is and entity view display.
$entity_in_context = $section_storage_context->getContextValue();
if ($entity_in_context instanceof EntityViewDisplayInterface) {
$entity_type_id = $entity_in_context->getTargetEntityTypeId();
$bundle = $entity_in_context->getTargetBundle();
$entity = $this->sampleEntityGenerator->get($entity_type_id, $bundle);
break;
}
elseif ($entity_in_context instanceof EntityInterface) {
$entity = $section_storage_context->getContextValue();
break;
}
}
}
}
}
}
$contextFieldNameDefinition = ContextDefinition::create('string');
$contextFieldNameDefinition->setLabel("field_name");
$contextFieldName = new Context($contextFieldNameDefinition, $field_definition->getName());
$contexts['field_name'] = $contextFieldName;
$contexts['field_name'] = new Context(ContextDefinition::create('string'), $field_definition->getName());
$contexts = RequirementsContext::addToContext(["field_formatter"], $contexts);
$contextBundleDefinition = ContextDefinition::create('string');
$contextBundleDefinition->setLabel("bundle");
$bundle = ($entity) ? $entity->bundle() : $field_definition->getTargetBundle();
if (NULL === $bundle) {
// Generate a default bundle when it is missing,
@@ -261,22 +235,42 @@ abstract class ComponentFormatterBase extends FormatterBase {
// @todo better implementation with service 'entity_type.bundle.info'
$bundle = $this->findEntityBundleWithField($entity_type_id, $field_definition->getName());
}
$contextBundle = new Context($contextBundleDefinition, $bundle);
$contexts['bundle'] = $contextBundle;
$contexts['bundle'] = new Context(ContextDefinition::create('string'), $bundle);
if (!$entity) {
$entity = $this->sampleEntityGenerator->get($entity_type_id, $bundle);
}
$contexts['entity'] = EntityContext::fromEntity($entity);
if ((NULL === $contexts['entity']) ||
(NULL === $contexts['entity']->getContextValue()) ||
if (!$this->checkContextSanity($contexts, $entity_type_id, $bundle)) {
throw new \LogicException("The entity context is not properly set.");
}
return $contexts;
}
/**
* Check the sanity of the context.
*
* @param array<string, \Drupal\Core\Plugin\Context\ContextInterface> $contexts
* The contexts.
* @param string $entity_type_id
* The entity type id.
* @param string $bundle
* The bundle.
*
* @return bool
* TRUE if the context is sane.
*/
protected function checkContextSanity(array $contexts, string $entity_type_id, string $bundle) : bool {
if (!isset($contexts['entity']) || !($contexts['entity'] instanceof EntityContext)) {
return FALSE;
}
if ((NULL === $contexts['entity']->getContextValue()) ||
!($contexts['entity']->getContextValue() instanceof EntityInterface) ||
($contexts['entity']->getContextValue()->getEntityTypeId() !== $entity_type_id) ||
($contexts['entity']->getContextValue()->bundle() !== $bundle)
) {
throw new \LogicException("The entity context is not properly set.");
return FALSE;
}
return $contexts;
return TRUE;
}
/**
@@ -330,4 +324,50 @@ abstract class ComponentFormatterBase extends FormatterBase {
return $dependencies;
}
/**
* Get the entity context when in layout builder.
*
* @return array
* The entity and context.
*
* @see \Drupal\ui_patterns_field_formatters\Plugin\Field\FieldFormatter\ComponentFormatterBase::getComponentSourceContexts()
*/
protected function getLayoutBuilderEntityContext() : array {
$returned = [
"entity" => NULL,
"context" => [],
];
if (!$this->moduleHandler->moduleExists("layout_builder")) {
return $returned;
}
// Infer the entity context, when in layout builder.
$section_storage = $this->routeMatch->getParameter("section_storage");
if ($section_storage instanceof SectionStorageInterface) {
$section_storage_contexts = $section_storage->getContexts();
$returned["context"] = $section_storage_contexts;
if (array_key_exists("entity", $section_storage_contexts) && ($entity = $section_storage_contexts["entity"]->getContextValue())) {
$returned["entity"] = $entity;
return $returned;
}
// Search for the entity in the context, with another name.
foreach ($section_storage_contexts as $section_storage_context) {
if ($section_storage_context instanceof EntityContext) {
// Now check if it is and entity view display.
$entity_in_context = $section_storage_context->getContextValue();
if ($entity_in_context instanceof EntityViewDisplayInterface) {
$entity_type_id = $entity_in_context->getTargetEntityTypeId();
$bundle = $entity_in_context->getTargetBundle();
$returned["entity"] = $this->sampleEntityGenerator->get($entity_type_id, $bundle);
break;
}
elseif ($entity_in_context instanceof EntityInterface) {
$returned["entity"] = $section_storage_context->getContextValue();
break;
}
}
}
}
return $returned;
}
}
Loading