Skip to content

Commit

Permalink
Merge pull request #121 from mrjmd/feature/component_context_update
Browse files Browse the repository at this point in the history
Component context creation and handling update
  • Loading branch information
emacoti authored Jan 7, 2020
2 parents 1cbbec2 + 60c0fd0 commit aeb81d3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
5 changes: 2 additions & 3 deletions modules/pdb_twig/components/twig_node/TwigNode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\pdb_twig\twig_node\TwigNode;
namespace Drupal\pdb_twig\twig_node;

/**
* Provides custom build steps for the twig-node twig block.
Expand All @@ -9,9 +9,8 @@ class TwigNode {

public static function build($build, $config) {
// Related context node is available on the config.
// This requires to make the context available on main PdbBlock class.
$node = $config['contexts']['entity:node'];
$build['#title'] = $node['title'][0]['value'];
$build['#title'] = $node->getTitle();

return $build;
}
Expand Down
61 changes: 49 additions & 12 deletions src/Plugin/Block/PdbBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ public function build() {
$attached = array_merge_recursive($attached, $header);
}

if ($contexts = $this->getContexts()) {
$attached['drupalSettings']['pdb']['contexts'] = $this->getJsContexts($contexts);
$contexts = $this->getContexts();
if ($contexts) {
$contexts_values = $this->getContextsValues($contexts);
$this->configuration['contexts'] = $contexts_values;

$js_contexts = $this->getJsContexts($contexts_values);
$attached['drupalSettings']['pdb']['contexts'] = $js_contexts;
}

if (isset($this->configuration['pdb_configuration'])) {
// @todo Is there anything else unique to key off of besides uuid
$attached['drupalSettings']['pdb']['configuration'][$this->configuration['uuid']] = $this->configuration['pdb_configuration'];
Expand Down Expand Up @@ -141,21 +147,48 @@ public function attachPageHeader(array $component) {
}

/**
* Add serialized entity to the JS Contexts.
* Get the value of contexts.
*
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
* The contexts to get value.
*
* @return array
* An array of contexts values.
*/
protected function getContextsValues(array $contexts) {
$context_values = [];
foreach ($contexts as $key => $context) {
$data = $context->getContextData();
if ($data instanceof EntityAdapter) {
$this->addEntityContextValue($data, $context_values, $key);
}
else {
// Get the data value otherwise.
// TODO: Will this cover all the cases?
$context_values[$key] = $data->getValue();
}
}

return $context_values;
}

/**
* Add entity context value (entity object) to context values.
*
* @param \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $data
* The entity to serialize.
* @param array $js_contexts
* The full array of JS contexts.
* The context data.
* @param array $context_values
* An array with context values.
* @param string $key
* The context key.
*/
protected function addEntityJsContext(EntityAdapter $data, array &$js_contexts, $key) {
protected function addEntityContextValue(EntityAdapter $data, array &$context_values, $key) {
$entity = $data->getValue();
$entity_access = $entity->access('view', NULL, TRUE);
if (!$entity_access->isAllowed()) {
return;
}

foreach ($entity as $field_name => $field) {
// @var \Drupal\Core\Field\FieldItemListInterface $field
$field_access = $field->access('view', NULL, TRUE);
Expand All @@ -166,13 +199,13 @@ protected function addEntityJsContext(EntityAdapter $data, array &$js_contexts,
}
}

$js_contexts["$key:" . $entity->getEntityTypeId()] = $entity->toArray();
$context_values["$key:" . $entity->getEntityTypeId()] = $entity;
}

/**
* Get an array of serialized JS contexts.
*
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
* @param array $contexts
* The contexts to serialize.
*
* @return array
Expand All @@ -181,9 +214,13 @@ protected function addEntityJsContext(EntityAdapter $data, array &$js_contexts,
protected function getJsContexts(array $contexts) {
$js_contexts = [];
foreach ($contexts as $key => $context) {
$data = $context->getContextData();
if ($data instanceof EntityAdapter) {
$this->addEntityJsContext($data, $js_contexts, $key);
if (is_object($context) && method_exists($context, 'toArray')) {
// Get the array version of the entity context.
$js_contexts[$key] = $context->toArray();
}
else {
// Leave the context as it is otherwise.
$js_contexts[$key] = $context;
}
}
return $js_contexts;
Expand Down
17 changes: 11 additions & 6 deletions src/Plugin/Derivative/PdbBlockDeriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@ public function getDerivativeDefinitions($base_plugin_definition) {
*
* @return \Drupal\Core\Plugin\Context\ContextDefinition[]
* Array of context to be used by block module
*
* @todo where is this defined in block module
*/
protected function createContexts(array $contexts) {
$contexts_definitions = [];
if (isset($contexts['entity'])) {
// @todo Check entity type exists and fail!
$contexts_definitions['entity'] = new ContextDefinition('entity:' . $contexts['entity']);

// Support for old node entity context defintion.
// "entity: node" should now be defined "entity: entity:node".
if (isset($contexts['entity']) && $contexts['entity'] === 'node') {
// For some reason even if context_id is "node" it must be set "entity".
$contexts['entity'] = 'entity:node';
}
// @todo Dynamically handle unknown context definitions

foreach ($contexts as $context_id => $context_type) {
$contexts_definitions[$context_id] = new ContextDefinition($context_type);
}

return $contexts_definitions;
}

Expand Down

0 comments on commit aeb81d3

Please sign in to comment.