views_timeline-1.0.x-dev/src/Plugin/views/style/ViewsTimeline.php
src/Plugin/views/style/ViewsTimeline.php
<?php
namespace Drupal\views_timeline\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
/**
* Style plugin to render each item in a views_timeline.
*
* @ingroup views_style_plugins
*
* @ViewsStyle(
* id = "views_timeline",
* title = @Translation("Views Timeline"),
* help = @Translation("Display the results as a views_timeline."),
* theme = "views_view_views_timeline",
* display_types = {"normal"}
* )
*/
class ViewsTimeline extends StylePluginBase {
/**
* Does the style plugin allows to use style plugins.
*
* @var bool
*/
protected $usesRowPlugin = TRUE;
/**
* Does the style plugin support custom css class for the rows.
*
* @var bool
*/
protected $usesRowClass = TRUE;
/**
* Does the style plugin support grouping of rows.
*
* @var bool
*/
protected $usesGrouping = FALSE;
/**
* Does the style plugin for itself support to add fields to it's output.
*
* This option only makes sense on style plugins without row plugins, like
* for example table.
*
* @var bool
*/
protected $usesFields = TRUE;
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['row_class_custom'] = ['default' => ''];
$options['row_class_default'] = ['default' => TRUE];
$options['views_timeline_type'] = ['default' => ''];
$options['views_timeline_title'] = ['default' => 'default'];
$options['views_timeline_description1'] = ['default' => ''];
$options['views_timeline_description2'] = ['default' => ''];
$options['views_timeline_description3'] = ['default' => ''];
$options['views_timeline_description4'] = ['default' => ''];
$options['views_timeline_date'] = ['default' => 'default'];
$options['views_timeline_layout'] = ['default' => '1'];
$options['views_timeline_palette'] = ['default' => '1'];
$options['timeline_primary_color'] = ['default' => '#2D3A46'];
$options['timeline_primary_date_color'] = ['default' => '#ffffff'];
$options['timeline_primary_heading_color'] = ['default' => '#ffffff'];
$options['timeline_secondary_color'] = ['default' => '#2D3A46'];
$options['timeline_secondary_date_color'] = ['default' => '#ffffff'];
$options['timeline_secondary_heading_color'] = ['default' => '#ffffff'];
$options['timeline_accent_color'] = ['default' => '#ECECEC'];
$options['timeline_content_color'] = ['default' => '#ECECEC'];
$options['timeline_content_dark_color'] = ['default' => '#ECECEC'];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
// Wrap all the form elements to help style the form.
$form['views_timeline_wrapper'] = [
'#markup' => '<div id="views-views_timeline-form-wrapper">',
];
$fields = ['' => $this->t('None')] + $this->getNonExcludedFields();
$layouts = views_timeline_layouts();
$palettes = [];
$palettes['none'] = 'None';
for ($i = 1; $i <= 50; $i++) {
$palettes[$i] = 'Palette ' . $i;
}
$palettes['custom'] = 'Custom Palette';
// Create the drop down box so users can choose an available skin.
$form['views_timeline_title'] = [
'#type' => 'select',
'#title' => $this->t('Timeline title'),
'#options' => $fields,
'#prefix' => '<div class="timeline-flex-wrapper timeline-config-wrapper">',
'#default_value' => $this->options['views_timeline_title'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the title for the timeline. This field will be mapped to the timeline title for identification and context.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_icon'] = [
'#type' => 'select',
'#title' => $this->t('Timeline icon'),
'#options' => $fields,
'#default_value' => $this->options['views_timeline_icon'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the icon for the timeline. This field will be mapped to the timeline icon for identification and context.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_date'] = [
'#type' => 'select',
'#title' => $this->t('Timeline date'),
'#options' => $fields,
'#default_value' => $this->options['views_timeline_date'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the title for the timeline. This field will be mapped to the timeline date for identification and context.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_description1'] = [
'#type' => 'select',
'#title' => $this->t('Timeline description'),
'#options' => $fields,
'#default_value' => $this->options['views_timeline_description1'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the title for the timeline. This field will be mapped to the timeline description for identification and context.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_description2'] = [
'#type' => 'select',
'#title' => $this->t('Timeline description 2'),
'#options' => $fields,
'#default_value' => $this->options['views_timeline_description2'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the title for the timeline. This field will be mapped to the timeline description for identification and context.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_description3'] = [
'#type' => 'select',
'#title' => $this->t('Timeline description 3'),
'#options' => $fields,
'#default_value' => $this->options['views_timeline_description3'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the title for the timeline. This field will be mapped to the timeline description for identification and context.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_description4'] = [
'#type' => 'select',
'#title' => $this->t('Timeline description 4'),
'#options' => $fields,
'#default_value' => $this->options['views_timeline_description4'],
'#description' => $this->t('Select a field from the available fields in the view that will be used as the title for the timeline. This field will be mapped to the timeline description for identification and context.'),
'#suffix' => '</div>',
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_layout'] = [
'#type' => 'select',
'#title' => $this->t('Timeline layout'),
'#options' => $layouts,
'#default_value' => $this->options['views_timeline_layout'],
'#description' => $this->t('Choose the layout style for your timeline.'),
];
// Create the drop down box so users can choose an available skin.
$form['views_timeline_palette'] = [
'#type' => 'select',
'#title' => $this->t('Timeline color palette'),
'#options' => $palettes,
'#default_value' => $this->options['views_timeline_palette'],
'#description' => $this->t('Select a color palette for your timeline. Choose "Custom Palette" for personalized colors.'),
];
$form['timeline_primary_color'] = [
'#type' => 'color',
'#title' => $this->t('Primary Color'),
'#default_value' => $this->options['timeline_primary_color'],
'#prefix' => '<div class="timeline-flex-wrapper custom-palette-wrapper">',
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_primary_date_color'] = [
'#type' => 'color',
'#title' => $this->t('Primary Date Color'),
'#default_value' => $this->options['timeline_primary_date_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_primary_heading_color'] = [
'#type' => 'color',
'#title' => $this->t('Primary Heading Color'),
'#default_value' => $this->options['timeline_primary_heading_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_secondary_color'] = [
'#type' => 'color',
'#title' => $this->t('Secondary Color'),
'#default_value' => $this->options['timeline_secondary_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_secondary_date_color'] = [
'#type' => 'color',
'#title' => $this->t('Secondary Date Color'),
'#default_value' => $this->options['timeline_secondary_date_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_secondary_heading_color'] = [
'#type' => 'color',
'#title' => $this->t('Secondary Heading Color'),
'#default_value' => $this->options['timeline_secondary_heading_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_accent_color'] = [
'#type' => 'color',
'#title' => $this->t('Accent Color'),
'#default_value' => $this->options['timeline_accent_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_content_color'] = [
'#type' => 'color',
'#title' => $this->t('Timeline Content Color'),
'#default_value' => $this->options['timeline_content_color'],
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
$form['timeline_content_dark_color'] = [
'#type' => 'color',
'#title' => $this->t('Timeline Content Dark Color'),
'#default_value' => $this->options['timeline_content_dark_color'],
'#suffix' => '</div>',
'#states' => [
'visible' => [
':input[name="style_options[views_timeline_palette]"]' => ['value' => 'custom'],
],
],
];
// Add a library to style the form.
$form['#attached']['library'] = ['views_timeline/form'];
}
/**
* {@inheritdoc}
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
// Validate all views_timeline type plugins values.
}
/**
* {@inheritdoc}
*/
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
// Submit all views_timeline type plugins values.
}
/**
* Get an array of non-excluded fields for the current view.
*
* @return array
* An array of field names.
*/
protected function getNonExcludedFields() {
$fields = [];
// Get the list of all fields.
$all_fields = $this->view->display_handler->getHandlers('field');
// Loop through all fields.
foreach ($all_fields as $field_name => $field_handler) {
// Check if the field is excluded.
if (empty($field_handler->options['exclude'])) {
// Include the field in the result.
$fields[$field_name] = $field_handler->adminLabel();
}
}
return $fields;
}
}
