Commit aa4ea4d9 authored by Mark Fullmer's avatar Mark Fullmer

Add views configuration form

parent 18ff822d
......@@ -3,3 +3,4 @@ type: module
description: Make tables in WYSIWYG fields responsive
core: 8.x
package: Other
configure: responsive_tables_filter.settings
\ No newline at end of file
responsive_tables_filter.admin:
title: 'Responsive Tables'
parent: system.admin_config_content
description: Configure the Tablesaw responsive table behavior
route_name: responsive_tables_filter.settings
weight: 100
\ No newline at end of file
......@@ -46,10 +46,14 @@ function responsive_tables_filter_theme() {
* Adds tablesaw JS when tables present and CSS/JS aggregation is off to table.
*/
function responsive_tables_filter_preprocess_views_view_table(&$variables) {
// Add tablesaw classes & data attribute.
$variables['attributes']['class'][] = 'tablesaw';
$variables['attributes']['class'][] = 'tablesaw-stack';
$variables['attributes']['data-tablesaw-mode'][] = 'stack';
$variables['view']->element['#attached']['library'][] = 'responsive_tables_filter/tablesaw-filter';
$config = \Drupal::config('responsive_tables_filter.settings');
if ($enabled = $config->get('views_enabled')) {
$mode = $config->get('views_tablesaw_mode');
// Add tablesaw classes & data attribute.
$variables['attributes']['class'][] = 'tablesaw';
$variables['attributes']['class'][] = 'tablesaw-' . $mode;
$variables['attributes']['data-tablesaw-mode'][] = $mode;
$variables['view']->element['#attached']['library'][] = 'responsive_tables_filter/tablesaw-filter';
$variables['#cache']['tags'][] = 'config:responsive_tables_filter.settings';
}
}
responsive_tables_filter.settings:
path: 'admin/config/content/responsive_tables_filter'
defaults:
_form: '\Drupal\responsive_tables_filter\Form\TablesawSettings'
_title: 'Responsive Tables'
requirements:
_permission: 'administer site configuration'
\ No newline at end of file
<?php
namespace Drupal\responsive_tables_filter\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\responsive_tables_filter\Plugin\Filter\FilterResponsiveTablesFilter;
/**
* Configure settings for the Responsive Tables Filter module.
*/
class TablesawSettings extends ConfigFormBase {
const SETTINGS = 'responsive_tables_filter.settings';
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'responsive_tables_filter_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);
$form['views_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Automatically add to all table-based Drupal views'),
'#description' => $this->t('For more fine-grained control of which Views tables should use the Tablesaw library, leave this unchecked and add the table attributes programmatically. See https://github.com/filamentgroup/tablesaw'),
'#default_value' => $config->get('views_enabled'),
];
$form['views_tablesaw_mode'] = [
'#type' => 'select',
'#title' => $this->t('Tablesaw mode'),
'#default_value' => $config->get('views_tablesaw_mode') ?? 'stack',
'#description' => $this->t('This will apply to all Views-generated tables on the site. See documentation: https://github.com/filamentgroup/tablesaw'),
'#options' => FilterResponsiveTablesFilter::$modes,
'#states' => [
'visible' => [
':input[name="views_enabled"]' => ['checked' => TRUE],
],
],
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->getEditable(static::SETTINGS)
->set('views_enabled', $form_state->getValue('views_enabled'))
->set('views_tablesaw_mode', $form_state->getValue('views_tablesaw_mode'))
->save();
parent::submitForm($form, $form_state);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment