Skip to content
Snippets Groups Projects
Commit 4f52bccc authored by Mikael Meulle's avatar Mikael Meulle
Browse files

Issue #3477106 by just_like_good_vibes: unable to properly use derivable...

Issue #3477106 by just_like_good_vibes: unable to properly use derivable contexts in a views row style options
parent b96ad11e
Branches
Tags
1 merge request!226Resolve #3477106 "2.0.0 beta3 unable to"
Pipeline #296768 passed with warnings
......@@ -21,7 +21,7 @@ use Drupal\views\ViewExecutable;
tags: ['views'],
context_requirements: ['views:row'],
context_definitions: [
'entity' => new EntityContextDefinition('entity:view', label: new TranslatableMarkup('View')),
'ui_patterns_views:view_entity' => new EntityContextDefinition('entity:view', label: new TranslatableMarkup('View')),
]
)]
class ViewFieldSource extends ViewsSourceBase {
......
......@@ -20,7 +20,7 @@ use Drupal\views\ViewExecutable;
prop_types: ['slot'], tags: ['views'],
context_requirements: ['views:style'],
context_definitions: [
'entity' => new EntityContextDefinition('entity:view', label: new TranslatableMarkup('View')),
'ui_patterns_views:view_entity' => new EntityContextDefinition('entity:view', label: new TranslatableMarkup('View')),
]
)]
class ViewRowsSource extends ViewsSourceBase {
......
......@@ -18,7 +18,7 @@ use Drupal\views\ViewExecutable;
description: new TranslatableMarkup('The title of the view.'),
prop_types: ['string'],
context_definitions: [
'entity' => new EntityContextDefinition('entity:view', label: new TranslatableMarkup('View')),
'ui_patterns_views:view_entity' => new EntityContextDefinition('entity:view', label: new TranslatableMarkup('View')),
]
)]
class ViewTitleSource extends ViewsSourceBase {
......
......@@ -57,11 +57,13 @@ abstract class ViewsSourceBase extends SourcePluginBase {
if (isset($this->context["ui_patterns_views:view"])) {
return $this->getContextValue("ui_patterns_views:view");
}
return $this->getViewExecutable($this->getContextValue("entity"));
if (isset($this->context["ui_patterns_views:view_entity"])) {
return $this->getViewExecutable($this->getContextValue("ui_patterns_views:view_entity"));
}
}
catch (ContextException) {
return NULL;
}
return NULL;
}
/**
......
......@@ -4,9 +4,12 @@ declare(strict_types=1);
namespace Drupal\ui_patterns_views\Plugin\views\row;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
......@@ -32,6 +35,13 @@ class ComponentRow extends Fields {
use ViewsPluginUiPatternsTrait;
/**
* The sample entity generator.
*
* @var \Drupal\ui_patterns\Entity\SampleEntityGenerator
*/
protected $sampleEntityGenerator;
/**
* {@inheritdoc}
*/
......@@ -42,6 +52,7 @@ class ComponentRow extends Fields {
$plugin_definition,
);
$instance->initialize($container);
$instance->sampleEntityGenerator = $container->get('ui_patterns.sample_entity_generator');
return $instance;
}
......@@ -83,7 +94,7 @@ class ComponentRow extends Fields {
}
}
// Build ui patterns component form.
$form['ui_patterns'] = $this->componentSettingsForm($form, $form_state, $this->getComponentSourceContexts());
$form['ui_patterns'] = $this->componentSettingsForm($form, $form_state, $this->getViewRowsComponentSourceContexts());
$form['ui_patterns']["#component_validation"] = FALSE;
}
......@@ -96,9 +107,41 @@ class ComponentRow extends Fields {
* {@inheritdoc}
*/
public function render($row) {
return $this->buildComponentRenderable($this->getComponentConfiguration()['component_id'], $this->getViewRowsComponentSourceContexts($row));
}
/**
* Get the source contexts for the component.
*
* @param mixed $row
* The view row if relevant.
*
* @return array
* Source contexts.
*/
protected function getViewRowsComponentSourceContexts(mixed $row = NULL): array {
$context = $this->getComponentSourceContexts();
$context['ui_patterns_views:row:index'] = new Context(new ContextDefinition('integer'), $row->index ?? 0);
return $this->buildComponentRenderable($this->getComponentConfiguration()['component_id'], $context);
$entity = NULL;
$bundle = NULL;
$view = $this->view;
if ($row === NULL) {
$base_entity_type = $view->getBaseEntityType();
if ($base_entity_type instanceof EntityTypeInterface) {
$base_entity_type_id = "" . $base_entity_type->id();
$entity = $this->sampleEntityGenerator->get($base_entity_type_id, $this->findEntityBundle($base_entity_type_id));
$bundle = "";
}
}
else {
$context['ui_patterns_views:row:index'] = new Context(new ContextDefinition('integer'), $row->index ?? 0);
$entity = $row->_entity;
$bundle = ($row->_entity instanceof EntityInterface) ? $row->_entity->bundle() : "";
}
if ($entity instanceof EntityInterface) {
$context['entity'] = EntityContext::fromEntity($entity);
$context['bundle'] = new Context(new ContextDefinition('string'), $bundle);
}
return $context;
}
/**
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns_views;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
......@@ -71,7 +72,7 @@ trait ViewsPluginUiPatternsTrait {
// Build view entity context.
$view_entity = $this->entityTypeManager->getStorage('view')->load($view->id());
if ($view_entity instanceof ViewEntityInterface) {
$context['entity'] = EntityContext::fromEntity($view_entity);
$context['ui_patterns_views:view_entity'] = EntityContext::fromEntity($view_entity);
}
return $context;
}
......@@ -116,4 +117,39 @@ trait ViewsPluginUiPatternsTrait {
return NULL;
}
/**
* Find an entity bundle.
*
* @param string $entity_type_id
* The entity type id.
*
* @return string
* The bundle.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
protected function findEntityBundle(string $entity_type_id) : string {
// @todo better implementation with service 'entity_type.bundle.info'
$bundle = $entity_type_id;
$entity_type_definition = $this->entityTypeManager->getDefinition($entity_type_id);
if (!($entity_type_definition instanceof EntityTypeInterface)) {
return $bundle;
}
$bundle_entity_type = $entity_type_definition->getBundleEntityType();
if (NULL !== $bundle_entity_type) {
$bundle_list = $this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple();
if (count($bundle_list) > 0) {
foreach ($bundle_list as $bundle_entity) {
$bundle_to_test = "" . $bundle_entity->id();
if ($bundle_to_test) {
$bundle = $bundle_to_test;
break;
}
}
}
}
return $bundle;
}
}
......@@ -90,7 +90,9 @@ abstract class DerivableContextSourceBase extends SourcePluginBase {
*/
public function getPropValue(): mixed {
$source_plugin = $this->getSourcePlugin();
return ($source_plugin) ? $source_plugin->getPropValue() : [];
$definition = $this->propDefinition;
$prop_type = $definition['ui_patterns']['type_definition'];
return ($source_plugin) ? $source_plugin->getValue($prop_type) : [];
}
/**
......
......@@ -24,6 +24,9 @@ class FieldPropertySource extends FieldValueSourceBase {
*/
public function getPropValue(): mixed {
$items = $this->getEntityFieldItemList();
if ($items === NULL) {
return NULL;
}
$delta = (isset($this->context['ui_patterns:field:index'])) ? $this->getContextValue('ui_patterns:field:index') : 0;
$lang_code = (isset($this->context['ui_patterns:lang_code'])) ? ($this->getContextValue("ui_patterns:lang_code") ?? "und") : "und";
$property = $this->getCustomPluginMetadata('property');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment