Due https://www.drupal.org/project/cookieconsent/issues/2910802 visiting the Form Settings returns a warning if no cookie_policy value has been provided (i.e. the first time you visit the form settings).

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 227 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).

Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 212)
Drupal\Core\Entity\EntityStorageBase->load(NULL) (Line: 85)
entity_load('node', NULL) (Line: 126)
Drupal\cookieconsent\Form\SettingsForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 514)
Drupal\Core\Form\FormBuilder->retrieveForm('cookieconsent_settings_form', Object) (Line: 271)
Drupal\Core\Form\FormBuilder->buildForm('cookieconsent_settings_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Conditionally check if $config->get('cookie_policy') exists before calling entity_load(),

Comments

gambry created an issue. See original summary.

gambry’s picture

gambry’s picture

Title: array_flip(): Can only flip STRING and INTEGER values! » PHP Warning on SettingsForm when cookie_policy not set
gambry’s picture

Status: Active » Needs review
opi’s picture

Status: Needs review » Reviewed & tested by the community

Simple onleline, tested and approved, thanks !

noita’s picture

StatusFileSize
new899 bytes

Entity load is now deprecated and then if you pass a patch it's better to change call function. I have change line to be ok with drupal compliance.

Hope than can help.

PS: sry for my english

villette’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.07 KB

The entity_load() function is deprecated indeed, but in this case, the best way to use the entity_type.manager service (entity.manager is also deprecated now) is to inject it into the block plugin as explained here.

gambry’s picture

Status: Needs review » Needs work
  1. +++ b/src/Form/SettingsForm.php
    @@ -116,6 +145,9 @@ class SettingsForm extends ConfigFormBase {
    +      $cookie_policy_node = $this->entityTypeManager->getStorage('node')->load($cookie_policy_nid);
    +    }
    

    $cookie_policy_nid is almost useless. Why don't just check if ($config->get('cookie_policy'))?

  2. +++ b/src/Form/SettingsForm.php
    @@ -116,6 +145,9 @@ class SettingsForm extends ConfigFormBase {
    +    if ($cookie_policy_nid = $config->get('cookie_policy')) {
    +      $cookie_policy_node = $this->entityTypeManager->getStorage('node')->load($cookie_policy_nid);
    +    }
    

    $cookie_policy_nid is almost useless. Why don't just check if ($config->get('cookie_policy'))?

  3. +++ b/src/Form/SettingsForm.php
    @@ -123,7 +155,7 @@ class SettingsForm extends ConfigFormBase {
    +      '#default_value' => isset($cookie_policy_node) ? $cookie_policy_node : NULL,
    

    I know it's going to be a long line, but can't we simplify the code and just use the ternary operator?

nitebreed’s picture

Status: Needs work » Fixed

Thank for the patch, I updated it with the changes mentioned in #8

  • Nitebreed committed 2c46dea on 8.x-1.x authored by villette
    Issue #2930734 by gambry, noita, villette, opi: PHP Warning on...

Status: Fixed » Closed (fixed)

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