Warning: Undefined array key 0 in eval() (line 2 of modules/contrib/custom_formatters/src/Plugin/CustomFormatters/FormatterType/PHP.php(40) : eval()'d code).

Created on 30 November 2023, 12 months ago
Updated 30 July 2024, 4 months ago

Problem/Motivation

Using custom formatter in a contentype, these warnings come after resetting facets, which show the search results

Steps to reproduce

Use searchapi, reset facets.

Warning: Undefined array key 0 in eval() (line 1 of modules/contrib/custom_formatters/src/Plugin/CustomFormatters/FormatterType/PHP.php(40) : eval()'d code).
Warning: Trying to access array offset on value of type null in eval() (line 1 of modules/contrib/custom_formatters/src/Plugin/CustomFormatters/FormatterType/PHP.php(40) : eval()'d code).

Proposed resolution

Remove the warnings by programming situation that undefined array wll not happen.
I am not a programmer sorry.
May be something like this is necessary: https://www.drupal.org/files/issues/2022-06-22/3291862-1-warning-undefin... โ†’

Thanks for your quick reply in advance, greetings,

๐Ÿ› Bug report
Status

Closed: works as designed

Version

4.0

Component

Engine: PHP

Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Summit
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chetan 11

    chetan 11 โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !4fixed โ†’ (Open) created by chetan 11
  • Status changed to Needs review 12 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chetan 11

    Hi,
    I have fixed the above error on "custom formatter's module, please check the raised MR.
    Thanks.

  • Status changed to RTBC 11 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    I tested the MR, thanks!
    Greetings,

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi, when will this patch be committed please? I ask it so whether i have to change my composer.json, or can wait for the new .dev version?
    Greetings and a happy 2024!

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi, I thought the patch worked, but now under Drupal 10.3 with custom formatters, and adding https://git.drupalcode.org/project/custom_formatters/-/merge_requests/4.... to my composer.json
    the warning is:

    Warning: Trying to access array offset on value of type null in eval() (regel 2 van custom_formatters/src/Plugin/CustomFormatters/FormatterType/PHP.php(46)
    

    Greetings, Martijn

  • Status changed to Active 4 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi, could it be related to https://www.drupal.org/project/custom_formatters/issues/3404747 ๐Ÿ› Does not work with drupal 10 Needs review ?
    I did add this patch...and still got these warnings back going to drupal 10.3.1
    thanks for your reply in advance,
    greetings,

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi,
    I use custom formatters in relation to shurly. Latest Beta4 version Shurly.
    Could that have something to do with my problem of these warnings?
    This is my Custom Formatters code, for the field Link:

    $shorturl = $items->getValue()[0]['uri'];
    $shortitem = shurly_shorten($items->getValue()[0]['uri']);
    if (isset($shortitem['shortUrl'])) {
      $shorturl = $shortitem['shortUrl'];
    }
    $output  = '<a  href="' . $shorturl . '" class="btn btn-primary"  rel="nofollow" target="_blank"> Shortlink text </a>';
    return $output;
    

    Greetings,

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi,
    could this be a temperal solution to get rid of the warning?

     if (isset($data)) {
            $output = eval($data);
            }
    

    The complete changed code looks than like this:

    public function viewElements(FieldItemListInterface $items, $langcode) {
        ob_start();
        
        $data = $this->entity->get('data');
    
        if (is_array($data) && isset($data[0])) {
            $output = eval($data[0]);
        } else {
          if (isset($data)) {
            $output = eval($data);
            }
        }
    
        $output = !empty($output) ? $output : ob_get_contents();
        ob_end_clean();
    
        return empty($output) ? FALSE : $output;
    }
    

    It seems that the warning is gone with this change.
    Greetings,

  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi,
    And I also read, using eval() is not good practice, see: https://www.exakat.io/en/land-where-php-uses-eval/
    Sorry if I am wrong, I am not a programmer.
    greetings,

  • Status changed to Closed: works as designed 4 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands Summit

    Hi,

    It was indeed a failure on my part. Some custom formatters nodes got value, other not. The one without kicked in the warning!
    This did the trick with me:

    if ($items->getValue() && isset($items->getValue()[0]['uri'])) {
      $shorturl = $items->getValue()[0]['uri'];
      $shortitem = shurly_shorten($items->getValue()[0]['uri']);
      if (isset($shortitem['shortUrl'])) {
        $shorturl = $shortitem['shortUrl'];
      }
      $output  = '<a  href="' . $shorturl . '" class="btn btn-primary"  rel="nofollow" target="_blank"> โžค Actuele Tarieven e/o Boeken ! </a>';
      return $output;
    }
    

    Greetings,

Production build 0.71.5 2024