Skip to content
Snippets Groups Projects

Issue #3374860: Fix PHPStan errors

2 unresolved threads
Files
3
@@ -2,9 +2,13 @@
namespace Drupal\views_block_override\Plugin\views\display;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\Block\ViewsBlock;
use Drupal\views\Plugin\views\display\Block;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A block plugin that allows exposed filters to be configured.
@@ -28,9 +32,92 @@ use Drupal\views\Plugin\views\display\Block;
class ViewsBlockOverride extends Block {
/**
* {@inheritdoc}
* The view block to override.
*
* @var \Drupal\views\Plugin\Block\ViewsBlock
*/
protected ViewsBlock $block;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo;
/**
* Constructs a new Views Block Override instance.
*
* @param array<string, mixed> $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info service.
*/
protected function defineOptions() {
public function __construct(
array $configuration,
string $plugin_id,
mixed $plugin_definition,
EntityTypeManagerInterface $entity_type_manager,
BlockManagerInterface $block_manager,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$entity_type_manager,
$block_manager
);
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* Creates an instance of the plugin.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to pull out services used in the plugin.
* @param array<string, mixed> $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*
* @return static
* Returns an instance of this plugin.
*/
public static function create(
ContainerInterface $container,
array $configuration,
mixed $plugin_id,
mixed $plugin_definition,
): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('plugin.manager.block'),
$container->get('entity_type.bundle.info'),
);
}
    • Comment on lines +93 to +108
      Suggested change
      97 * @return static
      98 * Returns an instance of this plugin.
      99 */
      100 public static function create(ContainerInterface $container,
      101 array $configuration,
      102 mixed $plugin_id,
      103 mixed $plugin_definition): static {
      104 return new static(
      105 $configuration,
      106 $plugin_id,
      107 $plugin_definition,
      108 $container->get('entity_type.manager'),
      109 $container->get('plugin.manager.block'),
      110 $container->get('entity_type.bundle.info'),
      111 );
      112 }
      97 * @return self
      98 * Returns an instance of this plugin.
      99 */
      100 public static function create(ContainerInterface $container,
      101 array $configuration,
      102 mixed $plugin_id,
      103 mixed $plugin_definition): self {
      104 return new self(
      105 $configuration,
      106 $plugin_id,
      107 $plugin_definition,
      108 $container->get('entity_type.manager'),
      109 $container->get('plugin.manager.block'),
      110 $container->get('entity_type.bundle.info'),
      111 );
      112 }
Please register or sign in to reply
/**
* The defined block options.
*
* @return array<string,mixed>
* Returns options.
*/
protected function defineOptions(): array {
$options = parent::defineOptions();
$options['allow']['contains']['contextual_filter'] = ['default' => 'contextual_filter'];
$options['allow']['contains']['exposed_sort'] = ['default' => 'exposed_sort'];
@@ -41,16 +128,16 @@ class ViewsBlockOverride extends Block {
/**
* Returns plugin-specific settings for the block.
*
* @param array $settings
* @param array<string, mixed> $settings
* The settings of the block.
*
* @return array
* @return array<string, mixed>
* An array of block-specific settings to override the defaults provided in
* \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration().
*
* @see \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration()
*/
public function blockSettings(array $settings) {
public function blockSettings(array $settings): array {
$settings = parent::blockSettings($settings);
// All contextual filters can be overridden.
@@ -59,8 +146,8 @@ class ViewsBlockOverride extends Block {
$settings['contextual_filter'][$id]['enabled'] = FALSE;
$settings['contextual_filter'][$id]['value'] = '';
}
$settings['exposed_sort']['sort_by']['value'] = 'title';
$settings['exposed_sort']['sort_order']['value'] = 'DESC';
$settings['exposed_sort']['sort_by'] = 'title';
$settings['exposed_sort']['sort_order'] = 'DESC';
$pager = $this->view->display_handler->getOption('pager');
$settings['pager_id']['enabled'] = FALSE;
@@ -72,9 +159,14 @@ class ViewsBlockOverride extends Block {
/**
* Provide the summary for page options in the views UI.
*
* This output is returned as an array.
* @param mixed $categories
* The view categories.
* @param mixed $options
* The view options.
*
* This output is returned as an array.
*/
public function optionsSummary(&$categories, &$options) {
public function optionsSummary(mixed &$categories, mixed &$options): void {
parent::optionsSummary($categories, $options);
$filtered_allow = array_filter($this->getOption('allow'));
@@ -105,19 +197,20 @@ class ViewsBlockOverride extends Block {
*
* @param \Drupal\views\Plugin\Block\ViewsBlock $block
* The ViewsBlock plugin.
* @param array $form
* @param array<string, mixed> $form
* The form definition array for the block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* @return array<string, mixed>
* The renderable form array representing the entire configuration form.
*
* @see \Drupal\views\Plugin\Block\ViewsBlock::blockForm()
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException|\Drupal\Component\Plugin\Exception\PluginNotFoundException
* Throws error of plugin not found.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @see \Drupal\views\Plugin\Block\ViewsBlock::blockForm()
*/
public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $form_state) {
public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $form_state): array {
parent::blockForm($block, $form, $form_state);
$allow_settings = array_filter($this->getOption('allow'));
$block_configuration = $block->getConfiguration();
@@ -156,13 +249,13 @@ class ViewsBlockOverride extends Block {
// If a validator for entity type is set, create an autocomplete.
if (isset($item->options['validate']) &&
strpos($item->options['validate']['type'], 'entity:') !== FALSE) {
str_contains($item->options['validate']['type'], 'entity:')) {
$split = explode(':', $item->options['validate']['type']);
$entity_type = $split[1];
if (substr($entity_type, -5) == '_type') {
if (str_ends_with($entity_type, '_type')) {
$bundles = [];
foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo(substr($entity_type, 0, -5)) as $key => $bundle) {
foreach ($this->entityTypeBundleInfo->getBundleInfo(substr($entity_type, 0, -5)) as $key => $bundle) {
$bundles[$key] = $bundle['label'];
}
@@ -179,7 +272,7 @@ class ViewsBlockOverride extends Block {
$form['override'][$block->getDerivativeId()][$type][$id]['value']['#type'] = 'entity_autocomplete';
$form['override'][$block->getDerivativeId()][$type][$id]['value']['#target_type'] = $entity_type;
if ($value) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$storage = $this->entityTypeManager->getStorage($entity_type);
$entity = $storage->load($value);
$form['override'][$block->getDerivativeId()][$type][$id]['value']['#default_value'] = $entity;
}
@@ -260,34 +353,37 @@ class ViewsBlockOverride extends Block {
*
* @param \Drupal\views\Plugin\Block\ViewsBlock $block
* The ViewsBlock plugin.
* @param array $form
* @param array<string, mixed> $form
* The form definition array for the full block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see \Drupal\views\Plugin\Block\ViewsBlock::blockSubmit()
*/
public function blockSubmit(ViewsBlock $block, $form, FormStateInterface $form_state) {
public function blockSubmit(ViewsBlock $block, mixed $form, FormStateInterface $form_state): void {
parent::blockSubmit($block, $form, $form_state);
$overrides = $form_state->getValue(['override']);
$config = $block->getConfiguration();
foreach ($overrides[$block->getDerivativeId()] as $type => $values) {
if ($type === 'contextual_filter') {
foreach ($values as $id => $settings) {
// Save overridden contextual_filter when control field is enabled.
if ($settings['enabled']) {
$config[$type][$id] = [
'enabled' => TRUE,
'value' => $settings['value'],
];
}
// Remove overridden contextual_filter when control field is not
// enabled.
else {
if (isset($config[$type][$id])) {
unset($config[$type][$id]);
$derivativeId = $block->getDerivativeId();
if (is_string($derivativeId) && is_array($overrides)) {
foreach ($overrides[$derivativeId] as $type => $values) {
if ($type === 'contextual_filter') {
foreach ($values as $id => $settings) {
// Save overridden contextual_filter when control field is enabled.
if (is_array($settings) && $settings['enabled']) {
$config[$type][$id] = [
'enabled' => TRUE,
'value' => $settings['value'],
];
}
// Remove overridden contextual_filter when control field is not
// enabled.
else {
if (isset($config[$type][$id])) {
unset($config[$type][$id]);
}
}
}
}
@@ -311,8 +407,21 @@ class ViewsBlockOverride extends Block {
}
// Save when there is no control field.
else {
foreach ($values as $id => $value) {
$config[$type][$id] = $value;
// Save override when control field is enabled.
if (is_array($values) && isset($values['enabled'])) {
if ($values['enabled']) {
$config[$type] = $values;
}
// Remove override when control field is not enabled.
else {
unset($config[$type]);
}
}
// Save when there is no control field.
else {
foreach ($values as $id => $value) {
$config[$type][$id] = $value;
}
}
}
}
@@ -327,7 +436,7 @@ class ViewsBlockOverride extends Block {
* @param \Drupal\views\Plugin\Block\ViewsBlock $block
* The block plugin for views displays.
*/
public function preBlockBuild(ViewsBlock $block) {
public function preBlockBuild(ViewsBlock $block): void {
parent::preBlockBuild($block);
$config = $block->getConfiguration();
@@ -336,9 +445,9 @@ class ViewsBlockOverride extends Block {
$sorts = $this->view->display_handler->getOption('sorts');
// Get the selected exposed sort and set it as the only sort option.
if (!empty($sorts[$config['exposed_sort']['sort_by']['value']])) {
$exposed_sort = $sorts[$config['exposed_sort']['sort_by']['value']];
$exposed_sort['order'] = $config['exposed_sort']['sort_order']['value'];
if (!empty($sorts[$config['exposed_sort']['sort_by']])) {
$exposed_sort = $sorts[$config['exposed_sort']['sort_by']];
$exposed_sort['order'] = $config['exposed_sort']['sort_order'];
$this->view->display_handler->setOption('sorts', [$exposed_sort]);
}
}
@@ -354,9 +463,12 @@ class ViewsBlockOverride extends Block {
}
/**
* The display block handler returns the structure necessary for a block.
* The display block handler.
*
* @return array<mixed,NULL>
* Returns the structure necessary for a block.
*/
public function execute() {
public function execute(): ?array {
$config = $this->block->getConfiguration();
if (!empty($config['contextual_filter'])) {
$view_args = $this->view->args;
@@ -389,21 +501,21 @@ class ViewsBlockOverride extends Block {
/**
* {@inheritdoc}
*/
public function usesExposed() {
public function usesExposed(): bool {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function displaysExposed() {
public function displaysExposed(): bool {
return FALSE;
}
/**
* Provide the default form for setting options.
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
public function buildOptionsForm(mixed &$form, FormStateInterface $form_state): void {
parent::buildOptionsForm($form, $form_state);
if ($form_state->get('section') == 'allow') {
Loading