Problem/Motivation

Seeing PHP notices related to views
Notice: Trying to access array offset on value of type bool in Drupal\views\Plugin\views\PluginBase->setOptionDefaults() (line 187 of docroot/core/modules/views/src/Plugin/views/PluginBase.php)

Steps to reproduce

Have a view that uses search api location and a location filter

Proposed resolution

Resolve the notice

I believe this is coming from

SearchApiFilterLocation.php

Specifically

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();

    $options['plugin']['default'] = '';
    foreach ($this->locationInputManager->getDefinitions() as $id => $plugin) {
      $options["plugin-$id"]['default'] = [];
    }

    $options['value'] = [
      'contains' => [
        'value' => ['default' => ''],
        'distance' => [
          'contains' => [
            'from' => ['default' => ''],
            'to' => ['default' => ''],
          ],
        ],
      ],
    ];

    $options['require'] = FALSE; <----- problem, I believe

    return $options;
  }

Views, at least in Drupal 9.5 is expecting every options value to be an array.

From PluginBase.php (views)

  protected function setOptionDefaults(array &$storage, array $options) {
    foreach ($options as $option => $definition) {
      if (isset($definition['contains'])) {
        $storage[$option] = [];
        $this->setOptionDefaults($storage[$option], $definition['contains']);
      }
      else {
        $storage[$option] = $definition['default']; <----- where the notice is firing
      }
    }
  }

Comments

aaron.ferris created an issue. See original summary.

aaron.ferris’s picture

Issue summary: View changes
aaron.ferris’s picture

StatusFileSize
new608 bytes
aaron.ferris’s picture

Status: Active » Needs review
aaron.ferris’s picture

Issue summary: View changes
progga’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm that the patch from #3 works for me. Thanks a lot for the fix :)

progga’s picture

Additionally, this new `require` option introduced in #3070519 is missing a config schema definition. I am not sure if that should be bundled with this fix. Something like this resolves both issues:

diff --git a/modules/search_api_location_views/config/schema/search_api_location_views.schema.yml b/modules/search_api_location_views/config/schema/search_api_location_views.schema.yml
index e110ba7..934ec85 100644
--- a/modules/search_api_location_views/config/schema/search_api_location_views.schema.yml
+++ b/modules/search_api_location_views/config/schema/search_api_location_views.schema.yml
@@ -98,6 +98,9 @@ views.filter.search_api_location:
             to:
               type: string
               label: 'Distance to'
+    require:
+      type: boolean
+      label: 'Does it require successfull location resolving?'
 
 
 views.filter_value.search_api_location:
diff --git a/modules/search_api_location_views/src/Plugin/views/filter/SearchApiFilterLocation.php b/modules/search_api_location_views/src/Plugin/views/filter/SearchApiFilterLocation.php
index f1a7491..ff24b6f 100644
--- a/modules/search_api_location_views/src/Plugin/views/filter/SearchApiFilterLocation.php
+++ b/modules/search_api_location_views/src/Plugin/views/filter/SearchApiFilterLocation.php
@@ -85,7 +85,7 @@ class SearchApiFilterLocation extends FilterPluginBase {
       ],
     ];
 
-    $options['require'] = FALSE;
+    $options['require'] = ['default' => FALSE];
 
     return $options;
   }
rohan-sinha’s picture

Reviewed Patch #3 , issue has been resolved.

jeroent’s picture

jeroent’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x-1.x. Thanks!

jeroent’s picture

Opened a follow-up issue for the config schema: #3343987: Missing config schema for search_api_location require option

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.