Kirby\Cms\App::extendHooks(): Argument #1 ($hooks) must be of type array, bool given, called in /project/kirby/src/Cms/AppPlugins.php on line 107

I’m using this solution:

<?php

return [
	'hooks' => [
		'kirbytext:after' => function ( $text ) {            
			if ( strlen( $text ) > 0 ) {
				// Get current page host
				// (he attributes will only be set for external links)
				$site_host = parse_url( site()->url() )['host'];

				// Convert $text to DOM tree
				$dom = new DomDocument();
				$dom->loadHTML( $text );

				// Loop over all anchor elements
				foreach ( $dom->getElementsByTagName( 'a' ) as $link_el ) {
					// Parse link address
					$link_href = $link_el->getAttribute( 'href' );
					$link_parts = parse_url( $link_href );

					if (
						$link_parts &&
						isset( $link_parts['host'] ) &&
						isset( $link_parts['scheme'] )
					) {
						$link_host = $link_parts['host'];
						$link_scheme = $link_parts['scheme'];

						// Only continue if the link is external
						if (
							in_array( $link_scheme, [ 'http', 'https' ] ) &&
							$link_host !== $site_host
						) {
							// Create string of old link (to find and replace it later)
							$link_str = $dom->saveHTML( $link_el );

							// Add link attributes
							$link_el->setAttribute( 'rel', 'noopener noreferrer' );
							$link_el->setAttribute( 'target', '_blank' );

							// Create new link string
							$new_link_str = $dom->saveHTML( $link_el );

							// replace old link with new link in $text
							$text = str_replace( $link_str, $new_link_str, $text );
						}
					}
				}
			}       
			
			return $text;
		}
	]
];

Got this from Snorpey Codes to help extend link tags to add "target=_blank" and rel="noopener noreferrer".

It seems technically correct, but I’m getting this message instead: Kirby\Cms\App::extendHooks(): Argument #1 ($hooks) must be of type array, bool given, called in /project/kirby/src/Cms/AppPlugins.php on line 107.

	public function extend(array $extensions, Plugin $plugin = null): array
	{
		foreach ($this->extensions as $type => $registered) {
			if (isset($extensions[$type]) === true) {
				// Line 107
				$this->{'extend' . $type}($extensions[$type], $plugin);
			}
		}

		return $this->extensions;
	}

Can anyone tell me what’s happening?

Where did you put that return array?

The return array is in a separate config file (hooks.php) and included in the main config added as 'hooks' => require_once 'hooks.php'.

Then you shouldn’t use the hooks keyword again in your included file, but only return an array of hooks.

1 Like

A second pair of eyes really helps sometimes. :sweat_smile:

Thank you.

Ok. So, the extension works and modifies the html itself, but I’m still getting the same error when trying to save further changes:

The form could not be saved.

Exception: TypeError

Kirby\Cms\App::extendHooks(): Argument #1 ($hooks) must be of type array, bool given, called in /project/kirby/src/Cms/AppPlugins.php on line 107

Ok.

So, weird problem.

This works everywhere else but one section that acts as the blog.

All other pages are able to be created, updated and saved without any issue, except in this one section.

The post page can be created and saved as a draft or in review, but cannot be published. That’s when the error comes. I’ve tried removing all of the code out of the post template, removed fields from the post blueprint. Bupkis.

I’m having the same issue.
Saving a form in the Panel works fine apart from the blog/news section, I get the same error:

Kirby\Cms\App::extendHooks(): Argument #1 ($hooks) must be of type array, bool given, called in /public_html/kirby/src/Cms/AppPlugins.php on line 108

To get around this I use a fix suggested by @pixelijn in this post:

I followed the method for using multiple config files in the Kirby Guide:

What is the best method for using multiple files?
require_once 'path-to-file.php'
or
require __DIR__.'/path-to-file.php'

My current LIVE/Production setup is:
Kirby 3.8.0
PHP 8.0.28

This. Examples: https://github.com/getkirby/getkirby.com/blob/main/site/config/config.php

1 Like

thanks @texnixe