Skip to content
Snippets Groups Projects
Commit 8ed144e7 authored by Antonio De Marco's avatar Antonio De Marco
Browse files

Adding first implementation of hook suggestions #31

parent b729e65d
Branches
Tags
No related merge requests found
......@@ -122,7 +122,7 @@ class FieldTemplateProcessor implements FieldTemplateProcessorInterface {
*/
protected function getContext() {
return [
'type' => 'entity_display',
'type' => 'entity',
'field_name' => $this->getFieldName(),
'entity_type' => $this->variables['element']['#entity_type'],
'bundle' => $this->variables['element']['#bundle'],
......
......@@ -118,7 +118,7 @@ class Pattern extends RenderElement {
if (isset($element['#context']) && !empty($element['#context']) && is_array($element['#context'])) {
$context = $element['#context'];
if (!isset($context['type']) || empty($context['type'])) {
throw new PatternRenderException("Pattern context must specify a context 'type.");
throw new PatternRenderException("Pattern #context array must have a valid 'type' key.");
}
$element['#context'] = new PatternContext($context['type'], $element['#context']);
}
......
......@@ -10,7 +10,7 @@ namespace Drupal\ui_patterns\Element;
class PatternContext {
/**
* Pattern context type, also used to tag @UiPatternsSource plugins.
* Pattern context type.
*
* @var string
*/
......
......@@ -106,6 +106,7 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
$definition['theme variables'] = array_fill_keys(array_keys($definition['fields']), NULL);
$definition['theme variables']['attributes'] = [];
$definition['theme variables']['context'] = [];
}
/**
......@@ -166,6 +167,16 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
return $items;
}
/**
* {@inheritdoc}
*/
public function isPatternHook($hook) {
$definitions = array_filter($this->getDefinitions(), function ($definition) use ($hook) {
return $definition['theme hook'] == $hook;
});
return !empty($definitions);
}
/**
* Process 'custom hook theme' definition property.
*
......
......@@ -38,4 +38,15 @@ interface UiPatternsManagerInterface extends PluginManagerInterface {
*/
public function hookTheme();
/**
* Check whereas the given theme hook is an actual pattern hook.
*
* @param string $hook
* Theme hook.
*
* @return bool
* Whereas the given theme hook is an actual pattern hook.
*/
public function isPatternHook($hook);
}
......@@ -74,7 +74,7 @@ class UiPatternsValidation implements UiPatternsValidationInterface {
* Throw exception if plugin definition is not valid.
*/
public function assertAllowedFieldName($name) {
$not_allowed = ['id', 'type', 'theme'];
$not_allowed = ['id', 'type', 'theme', 'context'];
if (in_array($name, $not_allowed)) {
throw new PatternDefinitionException(sprintf('UI Pattern field name not be one of the following reserved keywords: %s.', implode(', ', $not_allowed)));
}
......
......@@ -59,7 +59,7 @@ pattern7:
preview: preview
pattern8:
throws: 'UI Pattern field name not be one of the following reserved keywords: id, type, theme.'
throws: 'UI Pattern field name not be one of the following reserved keywords: id, type, theme, context.'
id: pattern8
label: label
description: description
......
<?php
use Drupal\ui_patterns\Element\PatternContext;
/**
* Alter UI Patterns definitions.
*
......@@ -17,3 +19,15 @@ function hook_ui_patterns_info_alter(&$definitions) {
function hook_ui_patterns_ui_patterns_source_info_alter(&$definitions) {
$definitions['my_field_source']['tags'][] = 'new_tag';
}
/**
* Provide hook theme suggestions for patterns.
*
* @see ui_patterns_theme_suggestions_alter()
*/
function hook_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('entity')) {
$hook = $variables['theme_hook_original'];
$suggestions[] = $hook . '__entity__' . $context->getProperty('entity_type');
}
}
......@@ -5,6 +5,8 @@
* Contains ui_patterns.module.
*/
use Drupal\ui_patterns\Element\PatternContext;
/**
* Implements hook_theme().
*/
......@@ -21,3 +23,29 @@ function ui_patterns_theme() {
],
] + \Drupal::service('plugin.manager.ui_patterns')->hookTheme();
}
/**
* Implements hook_theme_suggestions_HOOK_alter()
*/
function ui_patterns_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (\Drupal::service('plugin.manager.ui_patterns')->isPatternHook($hook)) {
\Drupal::service('module_handler')->alter('ui_patterns_suggestions', $suggestions, $variables, $variables['context']);
}
}
/**
* Implements hook_ui_patterns_suggestions_alter().
*/
function ui_patterns_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('entity')) {
$hook = $variables['theme_hook_original'];
$entity_type = $context->getProperty('entity_type');
$bundle = $context->getProperty('bundle');
$view_mode = $context->getProperty('view_mode');
$suggestions[] = $hook . '__entity__' . $entity_type;
$suggestions[] = $hook . '__entity__' . $entity_type . '__' . $bundle;
$suggestions[] = $hook . '__entity__' . $entity_type . '__' . $view_mode;
$suggestions[] = $hook . '__entity__' . $entity_type . '__' . $bundle . '__' . $view_mode;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment