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

Integrate pattern element in view rendering #12

parent dfa94e7f
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\Form\PatternDisplayFormTrait;
use Drupal\ui_patterns\Plugin\UiPatternsSourceManager;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\row\Fields;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -116,4 +117,24 @@ class Pattern extends Fields {
$form_state->setValue('row_options', $settings);
}
/**
* Helper function: check for all conditions that make a field visible.
*
* @param \Drupal\views\Plugin\views\field\FieldPluginBase $field
* Field object.
* @param \Drupal\Component\Render\MarkupInterface|null $field_output
* Field output.
*
* @return bool
* TRUE if a field should be visible, FALSE otherwise.
*
* @see template_preprocess_pattern_views_row()
*/
public function isFieldVisible(FieldPluginBase $field, $field_output) {
$empty_value = $field->isValueEmpty($field_output, $field->options['empty_zero']);
$hide_field = !$empty_value || (empty($field->options['hide_empty']) && empty($this->options['hide_empty']));
$empty = empty($field->options['exclude']) && $hide_field;
return $empty && $this->getMappingDestination('views_row', $field->field, $this->options);
}
}
......@@ -8,11 +8,10 @@
/**
* Implements hook_theme().
*/
function ui_patterns_views_theme($existing, $type, $theme, $path) {
function ui_patterns_views_theme() {
return [
'pattern_views_row' => [
'variables' => ['view' => NULL, 'options' => [], 'row' => NULL],
'path' => $path . '/templates',
],
];
}
......@@ -24,34 +23,30 @@ function ui_patterns_views_theme($existing, $type, $theme, $path) {
* Theme variables.
*/
function template_preprocess_pattern_views_row(&$variables) {
/** @var \Drupal\views\ResultRow $row */
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
$options = $variables['options'];
$mapping = $options['pattern_mapping'];
/** @var \Drupal\ui_patterns_views\Plugin\views\row\Pattern $row_plugin */
$fields = [];
/** @var \Drupal\views\ResultRow $row */
$view = $variables['view'];
$row_plugin = $view->rowPlugin;
$options = $variables['options'];
$row = $variables['row'];
foreach ($view->field as $id => $field) {
$field_output = $view->style_plugin->getField($row->index, $id);
$empty = $field->isValueEmpty($field_output, $field->options['empty_zero']);
if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($variables['options']['hide_empty'])))) {
$fields[$id] = $field_output;
if ($row_plugin->isFieldVisible($field, $field_output)) {
$destination = $row_plugin->getMappingDestination('views_row', $id, $options);
$fields[$destination] = $field_output;
}
}
$variables['pattern'] = [];
if (!empty($fields)) {
$definition = \Drupal::service('plugin.manager.ui_patterns')->getDefinition($options['pattern']);
$render = [
'#theme' => $definition['theme hook'],
$variables['pattern'] = [
'#type' => 'pattern',
'#id' => $options['pattern'],
'#fields' => $fields,
];
foreach ($fields as $name => $field) {
if (isset($mapping["views_row:{$name}"])) {
$settings = $mapping["views_row:{$name}"];
$render["#{$settings['destination']}"] = $field;
}
}
$variables['pattern'] = $render;
}
}
......@@ -155,6 +155,27 @@ trait PatternDisplayFormTrait {
}
}
/**
* Helper function: return mapping destination given plugin id and field name.
*
* @param string $plugin
* Current plugin ID.
* @param string $source
* Source field name.
* @param array $settings
* Setting array.
*
* @return string|null
* Destination field or NULL if none found.
*/
public function getMappingDestination($plugin, $source, array $settings) {
$mapping_id = $plugin . UiPatternsSourceBase::DERIVATIVE_SEPARATOR . $source;
if (isset($settings['pattern_mapping'][$mapping_id])) {
return $settings['pattern_mapping'][$mapping_id]['destination'];
}
return NULL;
}
/**
* Helper function: get default value.
*
......
......@@ -117,6 +117,28 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
}, $definition['fields']);
}
/**
* {@inheritdoc}
*/
public function hookTheme() {
$items = [];
foreach ($this->getDefinitions() as $definition) {
$hook = $definition['theme hook'];
$items[$hook] = [
'variables' => $definition['theme variables'],
];
if (!$definition['custom theme hook'] && $this->moduleHandler->moduleExists($definition['provider'])) {
/** @var \Drupal\Core\Extension\Extension $module */
$module = $this->moduleHandler->getModule($definition['provider']);
$items[$hook]['path'] = $module->getPath() . '/templates';
}
}
return $items;
}
/**
* {@inheritdoc}
*/
......
......@@ -39,4 +39,14 @@ interface UiPatternsManagerInterface extends PluginManagerInterface {
*/
public function getPatternFieldsOptions($id);
/**
* Build and return pattern theme definitions.
*
* @return array
* Theme definitions.
*
* @see ui_patterns_theme()
*/
public function hookTheme();
}
......@@ -8,31 +8,16 @@
/**
* Implements hook_theme().
*/
function ui_patterns_theme($existing, $type, $theme, $path) {
$items = [
function ui_patterns_theme() {
return [
'patterns_overview_page' => [
'variables' => ['patterns' => NULL],
'path' => $path . '/templates',
],
'patterns_single_page' => [
'variables' => ['pattern' => NULL],
'path' => $path . '/templates',
],
'patterns_meta_information' => [
'variables' => ['pattern' => NULL],
'path' => $path . '/templates',
],
];
$definitions = \Drupal::service('plugin.manager.ui_patterns')->getDefinitions();
foreach ($definitions as $definition) {
$items[$definition['theme hook']] = [
'variables' => $definition['theme variables'],
];
if (!$definition['custom theme hook'] && \Drupal::moduleHandler()->moduleExists($definition['provider'])) {
$items[$definition['theme hook']]['path'] = drupal_get_path('module', $definition['provider']) . '/templates';
}
}
return $items;
] + \Drupal::service('plugin.manager.ui_patterns')->hookTheme();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment