Problem/Motivation

Type Error when saving configuration.

The website encountered an unexpected error. Please try again later.
TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 274 of modules/contrib/entity_usage/src/Form/EntityUsageSettingsForm.php).
array_filter(NULL) (Line: 274)
Drupal\entity_usage\Form\EntityUsageSettingsForm->submitForm(Array, Object)

Steps to reproduce

Configuration -> Content Authoring -> Entity Usage Settings
Select a desired Field from the ENABLED LOCAL TASKS
Save Configuration.

Proposed resolution

Apply the uploaded patch.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lucian_pana created an issue. See original summary.

lucian_pana’s picture

Title: TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 274 of modules/contrib/entity_usage/src/Form/EntityUsageSettingsForm.php). » TypeError on array_filter()
lucian_pana’s picture

Issue summary: View changes
samitk’s picture

Assigned: Unassigned » samitk
Status: Active » Needs work
samitk’s picture

Assigned: samitk » Unassigned
Status: Needs work » Postponed (maintainer needs more info)

Hi @lucian_pana,

I tried to reproduce it, but not able to.

I am not sure why are you getting this error because in the following form field, the default value is array. SO the null given in array_filter() is not possible.

    $form['local_task_enabled_entity_types']['entity_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Local task entity types'),
      '#options' => $tabs_options,
      '#default_value' => $config->get('local_task_enabled_entity_types') ?: [],
    ];

Please check again.

Thanks
Samit K.

drumanuel’s picture

Same problem here

* edit: removing module paragraphs_library seemed to fix this. *

Entity Usage 8.x-2.0-beta12
Drupal core 10.2.2

TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 273 of modules/contrib/entity_usage/src/Form/EntityUsageSettingsForm.php).

Saving form causes this error out of the box.

config value local_task_enabled_entity_types is not returned by $this->config('entity_usage.settings') , breaking this code
array_filter($config->get('local_task_enabled_entity_types')) !== array_filter($form_state->getValue('local_task_enabled_entity_types')['entity_types']);

Full error log:
Drupal\entity_usage\Form\EntityUsageSettingsForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 129)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
Drupal\Core\Form\FormBuilder->processForm('entity_usage_settings_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

dineshkumarbollu’s picture

StatusFileSize
new36.09 KB

Hi
I try to reproduce the issue in Drupal 10.2 with Entity Usage 8.x-2.0-beta12 i am not getting any error. Configurations saved successfully.

if you still facing issue try this changes in the form.

$local_task_enabled_entity_types = $config->get('local_task_enabled_entity_types');
$local_tasks_updated = array_filter($local_task_enabled_entity_types ?? []) !== array_filter($form_state->getValue('local_task_enabled_entity_types')['entity_types']);
stmh’s picture

I am experiencing the same error. The provided snippet fixes it for me.

smulvih2’s picture

Status: Postponed (maintainer needs more info) » Needs review

The issue is with this code on line 273:

array_filter($config->get('local_task_enabled_entity_types'))

When you first install the module and go to configure it, $config->get('local_task_enabled_entity_types') will be NULL. So adding a simple ?? [] to ensure array_filter() has an empty array in this case would make sense.

smulvih2’s picture

StatusFileSize
new911 bytes
mortona2k’s picture

Status: Needs review » Reviewed & tested by the community

#10 fixed the error for me.

alexpott’s picture

Version: 8.x-2.0-beta12 » 8.x-2.x-dev
Status: Reviewed & tested by the community » Needs work

#9 is not correct - when you first install the module $config->get('local_task_enabled_entity_types') will return an empty array.

$ vendor/bin/drush en entity_usage -y
 [success] Module entity_usage has been installed. (Permissions - Configure)

$ vendor/bin/drush ev "var_dump(\Drupal::configFactory()->getEditable('entity_usage.settings')->get('local_task_enabled_entity_types'));"
array(0) {
}

The reason this issue exists is because #2932559: Make it easier to access entity usage list of an entity did not have an upgrade path. We could add one here. It'd be something like

/**
 * Initialize the "local_task_enabled_entity_types" config value to an empty array.
 */
function entity_usage_update_8207(&$sandbox) {
  $config = \Drupal::configFactory()->getEditable('entity_usage.settings');
  $entity_types = $config->get('local_task_enabled_entity_types');
  if ($entity_types === NULL) {
    $config->set('local_task_enabled_entity_types', [])->save(TRUE);
  }
}
alexpott’s picture

Status: Needs work » Needs review

Used a post update so we got config sorting for free.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

This fixes the problem by providing the missing update path.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Merging...

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed c90b019c on 8.x-2.x
    [#3373215] bug: TypeError on array_filter()
    
    By: lucian_pana
    By:...

Status: Fixed » Closed (fixed)

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