You are here

class ViewsEFFieldset in Views Exposed Form Fieldset 8

Views EF Fieldset display extender plugin.

Plugin annotation


@ViewsDisplayExtender(
  id = "views_ef_fieldset",
  title = @Translation("Views EF Fieldset display extender"),
  help = @Translation("Views EF Fieldset settings for this view."),
  no_ui = FALSE
)

Hierarchy

Expanded class hierarchy of ViewsEFFieldset

File

src/Plugin/views/display_extender/ViewsEFFieldset.php, line 26

Namespace

Drupal\views_ef_fieldset\Plugin\views\display_extender
View source
class ViewsEFFieldset extends DefaultDisplayExtender {

  /**
   * The render object.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The constructor.
   *
   * @param array $configuration
   *   Site configuration.
   * @param string $plugin_id
   *   Plugin id.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\Core\Render\RendererInterface $render
   *   The render.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RendererInterface $render) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->renderer = $render;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('renderer'));
  }

  /**
   * {@inheritDoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    if ($form_state
      ->get('section') !== 'exposed_form_options') {
      return [];
    }
    $options = $this->options['views_ef_fieldset'];
    $defaults = $this
      ->getPluginDefinition();
    $form['views_ef_fieldset'] = [
      '#tree' => TRUE,
    ];
    $form['views_ef_fieldset']['enabled'] = [
      '#type' => 'checkbox',
      '#default_value' => isset($options['enabled']) ? $options['enabled'] : $defaults['views_ef_fieldset']['enabled']['default'],
      '#title' => t('Enable fieldset around exposed forms ?'),
    ];
    $form['views_ef_fieldset']['options'] = [
      '#type' => 'fieldset',
      '#title' => t('Exposed form fieldset options'),
      '#states' => [
        'visible' => [
          ':input[name="views_ef_fieldset[enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $exposed_fields = $this->view
      ->getHandlers('filter');
    foreach ($exposed_fields as $exposed_field_index => $exposed_field) {
      $exposed_fields[$exposed_field_index]['handler_type'] = 'filter';
      if ((bool) $exposed_field['exposed'] !== TRUE) {
        unset($exposed_fields[$exposed_field_index]);
      }
      elseif ((bool) $exposed_field['expose']['use_operator'] === TRUE) {
        $exposed_operator = [];
        $exposed_operator['handler_type'] = 'filter';
        $exposed_operator['id'] = $exposed_field['expose']['operator_id'];
        $exposed_operator['expose'] = [
          'label' => ($exposed_field['expose']['multiple'] ? $exposed_field['expose']['placeholder_text_multiple'] : $exposed_field['expose']['placeholder_text_single']) . ' (' . $exposed_field['expose']['label'] . ')',
        ];
        $exposed_fields[] = $exposed_operator;
      }
    }
    $sort_fields = $this->view
      ->getHandlers('sort');
    foreach ($sort_fields as $sort_fields_index => $sort_field) {
      $sort_fields[$sort_fields_index]['handler_type'] = 'sort';
      if ((bool) $sort_field['exposed'] !== TRUE) {
        unset($sort_fields[$sort_fields_index]);
      }
    }
    if (!empty($sort_fields)) {
      $sort_by = [];
      $sort_by['handler_type'] = 'sort';
      $sort_by['id'] = 'sort_by';
      $sort_by['expose'] = [
        'label' => 'Sort by',
      ];
      $exposed_fields[] = $sort_by;
    }
    if ($form['exposed_form_options']['expose_sort_order']['#default_value'] === 1 && count($sort_fields)) {
      $sort_order = [];
      $sort_order['handler_type'] = 'sort';
      $sort_order['id'] = 'sort_order';
      $sort_order['expose'] = [
        'label' => 'Sort order',
      ];
      $exposed_fields[] = $sort_order;
    }
    $submit_button = [];
    $submit_button['handler_type'] = 'buttons';
    $submit_button['id'] = 'submit';
    $submit_button['expose'] = [
      'label' => 'Submit button',
    ];
    $exposed_fields[] = $submit_button;
    if ($form['exposed_form_options']['reset_button']['#default_value'] === 1) {
      $reset_button = [];
      $reset_button['handler_type'] = 'buttons';
      $reset_button['id'] = 'reset';
      $reset_button['expose'] = [
        'label' => 'Reset button',
      ];
      $exposed_fields[] = $reset_button;
    }
    foreach (array_values($exposed_fields) as $exposed_field_index => $exposed_field) {
      $container = [];
      $container['handler_type'] = 'container';
      $container['type'] = 'container';
      $container['container_type'] = 'details';
      $container['weight'] = $exposed_field_index;
      $container['expose'] = [
        'label' => 'Container ' . $exposed_field_index,
      ];
      $container['id'] = 'container-' . $exposed_field_index;
      $exposed_fields[] = $container;
    }
    $data = [
      [
        'id' => 'root',
        'type' => 'container',
        'weight' => 0,
        'pid' => '',
        'label' => 'Root',
        'title' => isset($options['options']['sort']['root']['title']) ? $options['options']['sort']['root']['title'] : t('Filters'),
        'description' => $options['options']['sort']['root']['description'],
        'open' => isset($options['options']['sort']['root']['open']) ? (bool) $options['options']['sort']['root']['open'] : TRUE,
        'container_type' => isset($options['options']['sort']['root']['container_type']) ? $options['options']['sort']['root']['container_type'] : 'details',
      ],
    ];
    foreach ($exposed_fields as $index => $field) {
      $field_options = $options['options']['sort'][$field['id']];
      $label = $field['expose']['label'] ? $field['expose']['label'] : $field['id'];
      $data[] = [
        'id' => $field['id'],
        'weight' => isset($field_options['weight']) ? $field_options['weight'] : (isset($field->weight) ? $field->weight : $index - count($exposed_fields)),
        'pid' => empty($field_options['pid']) ? 'root' : $field_options['pid'],
        'label' => $label,
        'title' => isset($field_options['title']) ? $field_options['title'] : $label,
        'description' => isset($field_options['description']) ? $field_options['description'] : '',
        'open' => isset($field_options['open']) ? (bool) $field_options['open'] : FALSE,
        'type' => $field['handler_type'],
        'container_type' => isset($field_options['container_type']) ? $field_options['container_type'] : 'details',
      ];
    }
    $viewsEFFieldsetData = new ViewsEFFieldsetData($data);
    $table = [
      '#type' => 'table',
      '#header' => [
        t('Label'),
        t('Type'),
        t('Title'),
        t('Description'),
        t('Open'),
        t('Weight'),
      ],
      '#tabledrag' => [
        [
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'item-pid',
          'subgroup' => 'item-pid',
          'source' => 'item-id',
          'hidden' => FALSE,
        ],
        [
          'action' => 'depth',
          'relationship' => 'group',
          'group' => 'item-depth',
          'hidden' => FALSE,
        ],
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'item-weight',
        ],
      ],
    ];
    foreach ($viewsEFFieldsetData
      ->buildFlat() as $item) {
      $item = $item['item'];
      $indentation = [];
      if (isset($item['depth']) && $item['depth'] > 0) {
        $indentation = [
          '#theme' => 'indentation',
          '#size' => $item['depth'],
        ];
      }
      $title = $item['title'];
      if ($item['type'] === 'container') {
        if ($item['id'] === 'root') {
          $title = '<em>' . $title . '</em>';
        }
        $title = '<strong>' . $title . '</strong>';
      }
      $table[$item['id']] = [
        '#item' => $item,
        'item' => [
          '#prefix' => !empty($indentation) ? $this->renderer
            ->render($indentation) : '',
          '#markup' => $title,
          '#wrapper_attributes' => [
            'colspan' => $item['type'] === 'container' ? '' : [
              'colspan' => 5,
            ],
          ],
          'id' => [
            '#type' => 'hidden',
            '#default_value' => $item['id'],
            '#size' => 4,
            '#attributes' => [
              'class' => [
                'item-id',
              ],
            ],
          ],
          'pid' => [
            '#type' => 'hidden',
            '#default_value' => $item['pid'],
            '#size' => 4,
            '#attributes' => [
              'class' => [
                'item-pid',
              ],
            ],
          ],
          'depth' => [
            '#type' => 'hidden',
            '#default_value' => $item['depth'],
            '#attributes' => [
              'class' => [
                'item-depth',
              ],
            ],
          ],
          'type' => [
            '#type' => 'hidden',
            '#default_value' => $item['type'],
          ],
        ],
      ];
      if ($item['type'] === 'container') {
        $table[$item['id']] += [
          'container_type' => [
            '#type' => 'select',
            '#default_value' => $item['container_type'],
            '#options' => [
              'container' => 'Container',
              'details' => 'Fieldset',
              'vertical_tabs' => 'Vertical tabs',
            ],
          ],
          'title' => [
            '#type' => 'textfield',
            '#size' => 15,
            '#default_value' => $item['title'],
          ],
          'description' => [
            '#type' => 'textfield',
            '#size' => 15,
            '#default_value' => $item['description'],
          ],
          'open' => [
            '#type' => 'checkbox',
            '#default_value' => $item['open'],
          ],
        ];
      }
      $table[$item['id']] += [
        'weight' => [
          '#item' => $item,
          '#type' => 'weight',
          '#title' => $item['title'],
          '#delta' => count($data),
          '#title_display' => 'invisible',
          '#default_value' => $item['weight'],
          '#attributes' => [
            'class' => [
              'item-weight',
            ],
          ],
        ],
      ];
      if ($item['id'] !== 'root') {
        $table[$item['id']]['#attributes']['class'][] = 'draggable';
      }
    }
    $form['views_ef_fieldset']['options']['sort'] = $table;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginDefinition() {
    $options = parent::defineOptions();
    $options['views_ef_fieldset'] = [
      'enabled' => [
        'default' => FALSE,
        'bool' => TRUE,
      ],
      'options' => [],
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {

    // Only process options if this is an unrelated form.
    if ($form_state
      ->get('section') === 'exposed_form_options') {
      $views_ef_fieldset = $form_state
        ->getValue('views_ef_fieldset');
      foreach ($views_ef_fieldset['options']['sort'] as $key => $data) {
        $data += $data['item'];
        unset($data['item']);
        $views_ef_fieldset['options']['sort'][$key] = $data;
      }
      $this->options['views_ef_fieldset'] = $views_ef_fieldset;
    }
    parent::submitOptionsForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DisplayExtenderPluginBase::defaultableSections public function Static member function to list which sections are defaultable and what items each section contains. 1
DisplayExtenderPluginBase::defineOptionsAlter public function Provide a form to edit options for this plugin.
DisplayExtenderPluginBase::optionsSummary public function Provide the default summary for options in the views UI. 1
DisplayExtenderPluginBase::preExecute public function Set up any variables on the view prior to execution. 1
DisplayExtenderPluginBase::query public function Inject anything into the query that the display_extender handler needs. Overrides PluginBase::query 1
DisplayExtenderPluginBase::validateOptionsForm public function Validate the options form. Overrides PluginBase::validateOptionsForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$definition public property Plugins's definition
PluginBase::$displayHandler public property The display object this plugin is for.
PluginBase::$options public property Options for this plugin will be held here.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$usesOptions protected property Denotes whether the plugin has an additional options form. 8
PluginBase::$view public property The top object of a view. 1
PluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 14
PluginBase::defineOptions protected function Information about options for all kinds of purposes will be held here. 18
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::destroy public function Clears a plugin. Overrides ViewsPluginInterface::destroy 2
PluginBase::doFilterByDefinedOptions protected function Do the work to filter out stored options depending on the defined options.
PluginBase::filterByDefinedOptions public function Filter out stored options depending on the defined options. Overrides ViewsPluginInterface::filterByDefinedOptions
PluginBase::getAvailableGlobalTokens public function Returns an array of available token replacements. Overrides ViewsPluginInterface::getAvailableGlobalTokens
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::getProvider public function Returns the plugin provider. Overrides ViewsPluginInterface::getProvider
PluginBase::getRenderer protected function Returns the render API renderer. 1
PluginBase::globalTokenForm public function Adds elements for available core tokens to a form. Overrides ViewsPluginInterface::globalTokenForm
PluginBase::globalTokenReplace public function Returns a string with any core tokens replaced. Overrides ViewsPluginInterface::globalTokenReplace
PluginBase::INCLUDE_ENTITY constant Include entity row languages when listing languages.
PluginBase::INCLUDE_NEGOTIATED constant Include negotiated languages when listing languages.
PluginBase::init public function Initialize the plugin. Overrides ViewsPluginInterface::init 8
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::listLanguages protected function Makes an array of languages, optionally including special languages.
PluginBase::pluginTitle public function Return the human readable name of the display. Overrides ViewsPluginInterface::pluginTitle
PluginBase::preRenderAddFieldsetMarkup public static function Moves form elements into fieldsets for presentation purposes. Overrides ViewsPluginInterface::preRenderAddFieldsetMarkup
PluginBase::preRenderFlattenData public static function Flattens the structure of form elements. Overrides ViewsPluginInterface::preRenderFlattenData
PluginBase::queryLanguageSubstitutions public static function Returns substitutions for Views queries for languages.
PluginBase::setOptionDefaults protected function Fills up the options of the plugin with defaults.
PluginBase::summaryTitle public function Returns the summary of the settings in the display. Overrides ViewsPluginInterface::summaryTitle 6
PluginBase::themeFunctions public function Provide a full list of possible theme templates used by this style. Overrides ViewsPluginInterface::themeFunctions 1
PluginBase::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks 6
PluginBase::unpackOptions public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away. Overrides ViewsPluginInterface::unpackOptions
PluginBase::usesOptions public function Returns the usesOptions property. Overrides ViewsPluginInterface::usesOptions 8
PluginBase::validate public function Validate that the plugin is correct and can be saved. Overrides ViewsPluginInterface::validate 6
PluginBase::viewsTokenReplace protected function Replaces Views' tokens in a given string. The resulting string will be sanitized with Xss::filterAdmin. 1
PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT constant Query string to indicate the site default language.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.
ViewsEFFieldset::$renderer protected property The render object. Overrides PluginBase::$renderer
ViewsEFFieldset::buildOptionsForm public function Provide a form to edit options for this plugin. Overrides DisplayExtenderPluginBase::buildOptionsForm
ViewsEFFieldset::create public static function Creates an instance of the plugin. Overrides PluginBase::create
ViewsEFFieldset::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginBase::getPluginDefinition
ViewsEFFieldset::submitOptionsForm public function Handle any special handling on the validate form. Overrides DisplayExtenderPluginBase::submitOptionsForm
ViewsEFFieldset::__construct public function The constructor. Overrides PluginBase::__construct