Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to change exposed filter when create a field #6

Open
wants to merge 6 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Plugin/Field/FieldFormatter/ViewfieldDefaultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\views\Views;
use Drupal\Component\Serialization\Json;

/**
* Plugin implementation of the 'viewfield_default' formatter.
Expand All @@ -35,6 +36,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $item->getEntity();
list($view_name, $view_display) = explode('|', $item->vname, 2);
$view = Views::getView($view_name);
if (!empty($item->settings)) {
$settings = Json::decode($item->settings);
}
$elements[$delta] = array(
'#type' => 'viewfield',
'#view' => $view,
Expand All @@ -45,9 +49,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
'#entity_type' => $entity->getEntityTypeId(),
'#entity_id' => $entity->id(),
'#entity' => $entity,
'#exposed_settings' => $settings,
'#theme' => 'viewfield_formatter_default',
);
}
return $elements;
}

}
12 changes: 8 additions & 4 deletions src/Plugin/Field/FieldType/ViewfieldItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Views;


/**
* Plugin implementation of the 'viewfield' field type.
*
Expand Down Expand Up @@ -58,14 +57,16 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
'vname' => array(
'type' => 'varchar',
'not null' => FALSE,
// Views requires at least 96 chars for the view name and display, plus
// we need 1 for our delimiter. Round up to a common value of 128.
'length' => 128,
),
'vargs' => array(
'type' => 'varchar',
'not null' => FALSE,
'length' => 255, //viewfield_field_instance_settings_form_validate
'length' => 255,
),
'settings' => array(
'type' => 'text',
'size' => 'big',
),
),
);
Expand All @@ -79,6 +80,8 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
->setLabel(t('View name'));
$properties['vargs'] = DataDefinition::create('string')
->setLabel(t('View args'));
$properties['settings'] = DataDefinition::create('string')
->setLabel(t('View settings'));
return $properties;
}

Expand Down Expand Up @@ -127,4 +130,5 @@ public static function fieldSettingsFormValidate(array $form, FormStateInterface
}
}
}

}
145 changes: 140 additions & 5 deletions src/Plugin/Field/FieldWidget/ViewfieldWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormState;
use Drupal\Component\Utility\Html;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\SortArray;
use Drupal\views\Views;

/**
Expand All @@ -34,31 +38,135 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$field_settings = $this->getFieldSettings();
$options = $this->getPotentialReferences($field_settings);

$id = Html::getUniqueId('state-wrapper-' . $items->getName() . '-' . $delta);

$element['#field_name'] = $items->getName();
$element['vname'] = array(
'#type' => 'select',
'#title' => t('Views Field'),
'#options' => $options,
'#empty_value' => 0,
'#ajax' => array(
'callback' => array($this, 'viewsSettings'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => NULL,
),
'wrapper' => $id,
),
'#access' => !$field_settings['force_default'],
'#default_value' => isset($items[$delta]->vname) ? $items[$delta]->vname : NULL,
);

$element['settings_wrapper'] = array(
'#type' => 'fieldset',
);

$element['settings_wrapper']['settings_wrapper_form'] = array(
'#type' => 'html_tag',
'#tag' => 'div',
);

$element['settings_wrapper']['settings_wrapper_form']['settings'] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => array(
'id' => $id,
),
);

if (isset($items[$delta]->vname)) {
$view = explode('|', $items[$delta]->vname);
$viewInstance = $this->getView($view[0], $view[1]);
$itemSettings = [];
if (!empty($items[$delta]->settings)) {
$itemSettings = Json::decode($items[$delta]->settings);
}
if ($viewInstance) {
$element['settings_wrapper']['settings_wrapper_form']['settings'] = $this->getViewSettings($viewInstance, $view[1], $itemSettings);
$element['settings_wrapper']['settings_wrapper_form']['settings']['#attributes']['id'] = $id;
}
}

$element['vargs'] = array(
'#type' => 'textfield',
'#title' => t('Arguments'),
'#default_value' => isset($items[$delta]->vargs) ? $items[$delta]->vargs : NULL,
'#access' => !$field_settings['force_default'],
'#description' => t('A comma separated list of arguments to pass to the selected view. '),
'#description' => t('A comma separated list of arguments to pass to the selected view.'),
);

return $element;
}

/**
* Helper function for get exposed filter.
*/
public function getViewSettings($view, $display, $settings) {
$form_state = new FormState();
if ($settings) {
$form_state->setUserInput($settings);
}
$view->initHandlers();
$form = [];
// Let form plugins know this is for exposed widgets.
$form_state->set('exposed', TRUE);
$form['settings'] = [];

// Go through each handler and let it generate its exposed widget.
foreach ($view->display_handler->handlers as $type => $value) {
/** @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
foreach ($view->$type as $id => $handler) {
if ($handler->canExpose() && $handler->isExposed()) {
if ($handler->isAGroup()) {
$handler->groupForm($form, $form_state);
$id = $handler->options['group_info']['identifier'];
}
else {
$handler->buildExposedForm($form, $form_state);
}
if ($info = $handler->exposedInfo()) {
$form['#info']["$type-$id"] = $info;
}
}
}
}

foreach ($settings as $name => $set) {
if ($form[$name]) {
$form[$name]['#default_value'] = $set;
}
}

foreach ($form['#info'] as $info) {
if (isset($form[$info['value']])) {
$form[$info['value']]['#title'] = $info['label'];
}
}

$exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
$exposed_form_plugin->exposedFormAlter($form, $form_state);

unset($form['actions']);
return $form;
}

public function viewsSettings(array $form, FormStateInterface $form_state) {
$view = $form_state->getTriggeringElement()['#value'];
$view = explode('|', $view);
$viewInstance = $this->getView($view[0], $view[1]);
if ($viewInstance && is_a($viewInstance->getDisplay(), 'Drupal\viewfield\Plugin\views\display\ViewFieldDisplay')) {
return $this->getViewSettings($viewInstance, $view[1]);
}
return [];
}

/**
* Returns a select options list of views displays of enabled and allowed views.
*
* @param array @settings
* The field settings
* @param array $settings
* The field settings.
*
* @return array
* An array with the allowed and enabled views and displays.
Expand All @@ -75,7 +183,7 @@ protected function getPotentialReferences($settings) {
}
}
$options = array();
foreach ($views as $view_name => $view) {
foreach ($views as $view) {
$displays = $view->get('display');
foreach ($displays as $display) {
$options[$view->id() . '|' . $display['id']] = $view->id() . ' - ' . $display['display_title'];
Expand All @@ -84,4 +192,31 @@ protected function getPotentialReferences($settings) {
return $options;
}

/**
* Helper function for retrieve view.
*
* @param string $viewId
* View machinable name to load.
* @param string $display
* Display plugin to load.
*/
public function getView($viewId, $display) {
$view = Views::getView($viewId);
if ($view) {
$view->setDisplay($display);
return $view;
}
return FALSE;
}

/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $formState) {
foreach ($values as $key => $value) {
$values[$key]['settings'] = Json::encode($value['settings_wrapper']['settings_wrapper_form']['settings']);
}
return $values;
}

}
29 changes: 29 additions & 0 deletions src/Plugin/views/display/ViewFieldDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Drupal\viewfield\Plugin\views\display;

use Drupal\views\Plugin\views\display\DisplayPluginBase;

/**
* The plugin that handles an embed display.
*
* @ingroup views_display_plugins
*
* @todo: Wait until annotations/plugins support access methods.
* no_ui => !\Drupal::config('views.settings')->get('ui.show.display_embed'),
*
* @ViewsDisplay(
* id = "viewfield",
* title = @Translation("Viewfield"),
* help = @Translation("Provide a display which can be embedded using the views api."),
* theme = "views_view",
* uses_menu_links = FALSE
* )
*/
class ViewFieldDisplay extends DisplayPluginBase {

public function displaysExposed() {
return FALSE;
}

}
15 changes: 7 additions & 8 deletions viewfield.module
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

use \Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
* @file
* Defines a field type to display a view.
*/

use Drupal\Core\Url;

/**
* Implements hook_theme().
*/
Expand All @@ -24,8 +23,9 @@ function viewfield_theme() {
*/
function viewfield_preprocess_viewfield_formatter_default(&$variables) {
$element = $variables['element'];
$element['#view']->setExposedInput($element['#exposed_settings']);
$view_el = $element['#view']->preview($element['#view_display'], array($element['#view_arguments']));
$output = render ($view_el);
$output = render($view_el);
$variables['output'] = $output;
}

Expand All @@ -42,7 +42,7 @@ function viewfield_element_info() {
}

/**
* #pre_render callback for a viewfield field.
* pre_render callback for a viewfield field.
*
* @see viewfield_post_render()
*/
Expand Down Expand Up @@ -75,7 +75,7 @@ function viewfield_pre_render($element) {
}

/**
* #post_render callback for a viewfield field.
* post_render callback for a viewfield field.
*
* @see viewfield_pre_render()
*/
Expand All @@ -85,9 +85,8 @@ function viewfield_post_render($content, $element) {
return $content;
}


/**
* Perform argument replacement
* Perform argument replacement.
*/
function _viewfield_get_view_args($vargs, $entity_type, $entity) {
$args = array();
Expand Down