View source
<?php
namespace Drupal\ui_patterns_views\Plugin\views\row;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\Form\PatternDisplayFormTrait;
use Drupal\ui_patterns\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;
class Pattern extends Fields {
use PatternDisplayFormTrait;
protected $moduleHandler = NULL;
protected $patternsManager;
protected $sourceManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, UiPatternsManager $patterns_manager, UiPatternsSourceManager $source_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->patternsManager = $patterns_manager;
$this->sourceManager = $source_manager;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.ui_patterns'), $container
->get('plugin.manager.ui_patterns_source'), $container
->get('module_handler'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['hide_empty'] = [
'default' => FALSE,
];
$options['default_field_elements'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['default_field_elements'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Provide default field wrapper elements'),
'#default_value' => $this->options['default_field_elements'],
'#description' => $this
->t('If not checked, fields that are not configured to customize their HTML elements will get no wrappers at all for their field, label and field + label wrappers. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
];
$form['hide_empty'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide empty fields'),
'#default_value' => $this->options['hide_empty'],
'#description' => $this
->t('Do not display fields, labels or markup for fields that are empty.'),
];
$context = [
'view' => $this->view,
];
$this
->buildPatternDisplayForm($form, 'views_row', $context, $this->options);
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$settings = $form_state
->getValue('row_options');
self::processFormStateValues($settings);
$form_state
->setValue('row_options', $settings);
}
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
->hasMappingDestination('views_row', $field->field, $this->options);
}
}