On "Add simple block page" (/admin/structure/block/simple-block/add)

I get the following 2 warnings:

Notice: Trying to access array offset on value of type null in Drupal\simple_block\SimpleBlockEditForm->form() (line 54 of modules/contrib/simple_block/src/SimpleBlockEditForm.php).

Notice: Trying to access array offset on value of type null in Drupal\simple_block\SimpleBlockEditForm->form() (line 56 of modules/contrib/simple_block/src/SimpleBlockEditForm.php).

The problematic lines are these 2:

Line 54: '#format' => $simple_block->getContent()['format'],
Line 56: '#default_value' => $simple_block->getContent()['value'],

And this is happening because when creating a new block, the function "getContent()" returns Null.

A possible solution would be replacing the above lines these with
$simple_block_content = $simple_block->getContent();
'#format' => $simple_block_content ? $simple_block_content['format'] : '',
'#default_value' => $simple_block_content ? $simple_block_content['value'] : '',

(I'm on PHP 7.4 & Drupal 9 btw)

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

Zekvyrin created an issue. See original summary.

zekvyrin’s picture

Issue summary: View changes

zekvyrin’s picture

I created a merge request for this issue.

zekvyrin’s picture

Status: Active » Needs review
dsnopek’s picture

Status: Needs review » Reviewed & tested by the community

I also encountered this problem, and the patch fixed it for me!

vipin.mittal18’s picture

Thanks for the patch. I applied at one of my containers and working perfectly.

RTBC +1

poorva’s picture

this patch worked for me +1

chi’s picture

Status: Reviewed & tested by the community » Needs work

The patch resolves the notice. However it does not fix the root cause of the problem.

SimpleBlock::getContent() should always return array as declared in its interface.

zekvyrin’s picture

I'm sorry please ignore merge request !3 (it's something for another issue and I accidentally using a wrong fork).

@Chi is right, this wasn't compatible with the documentation. I created a new merge request (!5) that returns an empty array instead.

Also has a better result in the end, because now the default format is the first available format for the user.

zekvyrin’s picture

Status: Needs work » Needs review
zekvyrin’s picture

claudiu.cristea’s picture

Why not just doing this?

  public function getContent() {
    return $this->content ?: [
      'value' => '',
      'format' => filter_default_format(),
    ];
  }
chi’s picture

This is small, but should we include more logic to support unsupported PHP versions?

@Zekvyrin, Drupal core 8.x still supports PHP 7.0. Given that this module does not specify PHP requirements, users will expect it to work on any PHP versions that are supported by Drupal core.

Contributed modules do not have to support same PHP versions as Drupal core. But this needs to be declared explicitly in composer.json file and in module info file. This would block installing a module on unsupported platforms and make users less confused.

claudiu.cristea’s picture

claudiu.cristea’s picture

Status: Needs review » Fixed

Thanks you. Fixed.

Status: Fixed » Closed (fixed)

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