Skip to content
Snippets Groups Projects
Commit 8a301f76 authored by Mikael Meulle's avatar Mikael Meulle Committed by Pierre Dureau
Browse files

Issue #3462505 by just_like_good_vibes: Added view fields not available to the component till save

parent 909739cf
No related branches found
No related tags found
1 merge request!145When adding a new view field it's not available to the pattern till save
Pipeline #229810 passed with warnings
......@@ -36,7 +36,7 @@ class ViewFieldSource extends ViewsSourceBase {
$parents = ['settings', 'ui_patterns_views_field'];
$configuration = $this->getConfiguration();
$field_name = NestedArray::getValue($configuration, $parents);
$view = $this->getContextValue("ui_patterns_views:row:view");
$view = $this->getViewExecutable($this->getContextValue("ui_patterns_views:row:view"));
// Get row index inside the configuration.
$row_index = $this->getContextValue("ui_patterns_views:row:index") ?? 0;
if (empty($field_name) || empty($view) || (empty($row_index) && $row_index !== 0) || !$view instanceof ViewExecutable) {
......@@ -87,16 +87,15 @@ class ViewFieldSource extends ViewsSourceBase {
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$form = parent::settingsForm($form, $form_state);
$view = $this->getContextValue('entity');
$view = $this->getViewExecutable($this->getContextValue('entity'));
$current_display = $this->getContextValue('display');
$options = [];
foreach ($view->get('display') as $display_id => $display) {
if ($current_display !== $display_id || empty($display['display_options']['fields'])) {
continue;
}
foreach ($display['display_options']['fields'] as $field) {
$options[$field['id']] = !empty($field['label']) ? $field['label'] : $field['field'];
if ($view) {
$display = $view->getDisplay();
if ($display->getPluginId() == $current_display && is_array($display->options) && is_array($display->options['fields'])) {
foreach ($display->options['fields'] as $field_id => $field) {
$options[$field_id] = sprintf("%s (%s)", !empty($field['label']) ? $field['label'] : $field['field'], $field_id);
}
}
}
$form['ui_patterns_views_field'] = [
......
......@@ -31,8 +31,7 @@ class ViewTitleSource extends ViewsSourceBase {
if (empty($view) || !$view instanceof ViewExecutable) {
return '';
}
$view_title = $view->getTitle();
return (!$view_title) ? "" : $view->getTitle();
return $view->getTitle();
}
}
......@@ -6,15 +6,85 @@ namespace Drupal\ui_patterns_views\Plugin\UiPatterns\Source;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\SharedTempStore;
use Drupal\ui_patterns\SourcePluginBase;
use Drupal\views\Entity\View;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the source.
*/
abstract class ViewsSourceBase extends SourcePluginBase {
/**
* The views temp store.
*
* @var \Drupal\Core\TempStore\SharedTempStore|null
*/
protected ?SharedTempStore $tempStore = NULL;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|null
*/
protected ?ModuleHandlerInterface $moduleHandler;
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
) {
$plugin = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$plugin->moduleHandler = $container->get('module_handler');
if ($plugin->moduleHandler->moduleExists('views_ui')) {
$plugin->tempStore = $container->get('tempstore.shared')->get('views');
}
return $plugin;
}
/**
* Returns the view from context.
*
* @return \Drupal\views\ViewExecutable|null
* The view executable.
*/
protected function getView() : ?ViewExecutable {
try {
return $this->getViewExecutable($this->getContextValue("entity"));
}
catch (ContextException) {
return NULL;
}
}
/**
* Get the view executable.
*
* @param \Drupal\views\Entity\View|ViewExecutable $view
* The view entity or view executable.
*
* @return \Drupal\views\ViewExecutable|null
* The view executable.
*/
protected function getViewExecutable($view) : ?ViewExecutable {
if ($view && $this->tempStore && ($view_in_edition = $this->tempStore->get($view->id()))) {
return $view_in_edition->getExecutable();
}
return ($view instanceof View) ? $view->getExecutable() : $view;
}
/**
* {@inheritdoc}
......@@ -51,21 +121,4 @@ abstract class ViewsSourceBase extends SourcePluginBase {
return $output;
}
/**
* Returns the view from context.
*
* @return \Drupal\views\ViewExecutable|null
* The view executable.
*/
protected function getView() : ?ViewExecutable {
try {
/** @var \Drupal\views\Entity\View $viewEntity */
$viewEntity = $this->getContextValue("entity");
return ($viewEntity instanceof View) ? $viewEntity->getExecutable() : NULL;
}
catch (ContextException) {
return NULL;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment