Skip to content
Snippets Groups Projects

Issue #3325686: Term labels collected by PageContext are not full controlled by mapped fields

Open Issue #3325686: Term labels collected by PageContext are not full controlled by mapped fields
@@ -8,6 +8,7 @@ use Drupal\acquia_perz\Service\Helper\SettingsHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
@@ -284,20 +285,80 @@ class PageContext extends BaseContext {
* Node.
*/
private function setFields(NodeInterface $node): void {
$available_field_vocabulary_names = $this->getAvailableFieldVocabularyNames($node);
$vocabulary_term_names = $this->getVocabularyTermNames($node);
// Get a mapping that aggregates the udf user fields.
$field_mapping = $this->getContextFieldMapping($node);
// Find Field Term names.
foreach ($available_field_vocabulary_names as $page_context_name => $vocabulary_names) {
$field_term_names = $this->getFieldTermNames($vocabulary_names, $vocabulary_term_names);
// Get the set of field names for each context.
foreach ($field_mapping as $page_context_name => $field_names) {
// Get the context string for each set of field names.
$context_string = $this->getContextString($node, $field_names);
// Only set when the value is a populated array
// Empty arrays return as false in PHP.
if (!empty($field_term_names)) {
$this->htmlHeadContexts[$page_context_name] = implode(',', $field_term_names);
if (!empty($context_string)) {
$this->htmlHeadContexts[$page_context_name] = $context_string;
}
}
}
/**
* Get field names for each page context.
*
* @param \Drupal\node\NodeInterface $node
* The node being processed.
*
* @return array
* Associated array, keyed by page context, of field names for each context.
*/
private function getContextFieldMapping(NodeInterface $node) {
$field_mapping = [];
// Regular field mapping.
foreach ($this->fieldMappings as $page_context_name => $field_name) {
if ($node->hasField($field_name)) {
$field_mapping[$page_context_name][$field_name] = $field_name;
}
}
// The following 3 mappings have all the same structure with different
// array IDs, so we can merge them without conflict.
$udf_mappings = array_merge($this->udfPersonMappings, $this->udfTouchMappings, $this->udfEventMappings);
foreach ($udf_mappings as $page_context_name => $properties) {
$field_name = $properties['value'];
if ($node->hasField($field_name)) {
$field_mapping[$page_context_name][$field_name] = $field_name;
}
}
return $field_mapping;
}
/**
* Collect all the term names for terms referenced in the list of fields.
*
* @param \Drupal\node\NodeInterface $node
* The node being processed.
* @param array $field_names
* The field names for this context.
*
* @return string
* The collected term names, comma delimited.
*/
private function getContextString(NodeInterface $node, array $field_names) {
$labels = [];
// Iterate through the field names and use the available method on
// entity reference fields to then iterate the referenced entities and
// collect their labels.
foreach ($field_names as $field_name) {
$item_list = $node->get($field_name);
if ($item_list instanceof EntityReferenceFieldItemListInterface) {
foreach ($item_list->referencedEntities() as $entity) {
$labels[] = preg_replace('/,\s*/', ' ', $entity->label());
}
}
}
return implode(',', $labels);
}
/**
* Get available Fields and their vocabulary names within the node.
*
Loading