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

Add pattern suggestions for views, ds fields and fieldgroups #31

parent 51d0279b
Branches
Tags
No related merge requests found
......@@ -122,7 +122,7 @@ class FieldTemplateProcessor implements FieldTemplateProcessorInterface {
*/
protected function getContext() {
return [
'type' => 'entity',
'type' => 'ds_field_template',
'field_name' => $this->getFieldName(),
'entity_type' => $this->variables['element']['#entity_type'],
'bundle' => $this->variables['element']['#bundle'],
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\Form\PatternDisplayFormTrait;
use Drupal\ui_patterns\Element\PatternContext;
/**
* Implements hook_theme().
......@@ -61,3 +62,22 @@ function ui_patterns_ds_field_overview_submit($form, FormStateInterface $form_st
function template_preprocess_field__pattern_ds_field_template(&$variables) {
\Drupal::service('ui_patterns_ds.field_template_processor')->process($variables);
}
/**
* Implements hook_ui_patterns_suggestions_alter().
*/
function ui_patterns_ds_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('ds_field_template')) {
$hook = $variables['theme_hook_original'];
$field_name = $context->getProperty('field_name');
$entity_type = $context->getProperty('entity_type');
$bundle = $context->getProperty('bundle');
$view_mode = $context->getProperty('view_mode');
$suggestions[] = $hook . '__ds_field_template__' . $field_name;
$suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type;
$suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $bundle;
$suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $view_mode;
$suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $bundle . '__' . $view_mode;
}
}
......@@ -87,6 +87,13 @@ class PatternFormatter extends FieldGroupFormatterBase implements ContainerFacto
$element['#type'] = 'pattern';
$element['#id'] = $this->getSetting('pattern');
$element['#fields'] = $fields;
$element['#context'] = [
'type' => 'field_group',
'group_name' => $this->configuration['group']->group_name,
'entity_type' => $this->configuration['group']->entity_type,
'bundle' => $this->configuration['group']->bundle,
'view_mode' => $this->configuration['group']->mode,
];
}
/**
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\Element\PatternContext;
/**
* Implements hook_form_FORM_ID_alter().
......@@ -45,3 +46,22 @@ function ui_patterns_field_group_field_group_field_overview_submit($form, FormSt
$form_state->set('field_group', $field_group_form_state);
}
}
/**
* Implements hook_ui_patterns_suggestions_alter().
*/
function ui_patterns_field_group_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('field_group')) {
$hook = $variables['theme_hook_original'];
$group_name = $context->getProperty('group_name');
$entity_type = $context->getProperty('entity_type');
$bundle = $context->getProperty('bundle');
$view_mode = $context->getProperty('view_mode');
$suggestions[] = $hook . '__field_group__' . $group_name;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type . '__' . $bundle;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type . '__' . $view_mode;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type . '__' . $bundle . '__' . $view_mode;
}
}
......@@ -5,6 +5,8 @@
* Contains module file.
*/
use Drupal\ui_patterns\Element\PatternContext;
/**
* Implements hook_layout_alter().
*/
......@@ -29,3 +31,22 @@ function ui_patterns_layouts_layout_alter(&$definitions) {
$definitions['pattern__' . $pattern_definition['id']] = $definition;
}
}
/**
* Implements hook_ui_patterns_suggestions_alter().
*/
function ui_patterns_layouts_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('layout')) {
$hook = $variables['theme_hook_original'];
$group_name = $context->getProperty('group_name');
$entity_type = $context->getProperty('entity_type');
$bundle = $context->getProperty('bundle');
$view_mode = $context->getProperty('view_mode');
$suggestions[] = $hook . '__field_group__' . $group_name;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type . '__' . $bundle;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type . '__' . $view_mode;
$suggestions[] = $hook . '__field_group__' . $group_name . '__' . $entity_type . '__' . $bundle . '__' . $view_mode;
}
}
......@@ -5,6 +5,8 @@
* Contains module file.
*/
use Drupal\ui_patterns\Element\PatternContext;
/**
* Implements hook_theme().
*/
......@@ -47,6 +49,25 @@ function template_preprocess_pattern_views_row(&$variables) {
'#type' => 'pattern',
'#id' => $options['pattern'],
'#fields' => $fields,
'#context' => [
'type' => 'views_row',
'view_name' => $view->storage->id(),
'display' => $view->current_display,
],
];
}
}
/**
* Implements hook_ui_patterns_suggestions_alter().
*/
function ui_patterns_views_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('views_row')) {
$hook = $variables['theme_hook_original'];
$view_name = $context->getProperty('view_name');
$display = $context->getProperty('display');
$suggestions[] = $hook . '__views_row__' . $view_name;
$suggestions[] = $hook . '__views_row__' . $view_name . '__' . $display;
}
}
......@@ -5,8 +5,6 @@
* Contains ui_patterns.module.
*/
use Drupal\ui_patterns\Element\PatternContext;
/**
* Implements hook_theme().
*/
......@@ -28,24 +26,7 @@ function ui_patterns_theme() {
* 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)) {
if (\Drupal::service('plugin.manager.ui_patterns')->isPatternHook($hook) && !empty($variables['context'])) {
\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