Jump to content
dragan

Hooks: JS is not being injected

Recommended Posts

$this->addHookBefore('InputfieldTextarea::render', function($event) {
	$field = $event->object;
	if($field->name == 'body_offer') {
		$del = '';
		foreach($this->wire->pages->get(11229)->textblocks as $item) {
			$field->entityEncodeText = false;
			$body = str_replace(PHP_EOL, '', $item->body);
			$body = addslashes($body);
			$title = $item->title;
			$field->description .= $del . "<a href=\"#\" class=\"ckesnippet\" data-snippet=\"$body\">$title</a>";
			$del = ' | ';
		}
    }
    $js = wire('config')->urls->templates . 'scripts/ckesnippets.js';
    $event->return = str_replace("</html>", "\n<script type='text/javascript' src='$js'></script>\n</html>", $event->return);
});

I'm trying to add custom text-blocks from repeater fields into CKEditor. I've stolen the whole idea (and code) from @bernhard

Problem is: The links are actually being rendered, but the Javascript is not loaded, i.e. the $event->return line doesn't seem to work. If I inspect the HTML, my additional script-tag is nowhere to be seen. I don't get any errors either. Running latest PW dev + PHP 7.1.19.

  • Like 1

Share this post


Link to post
Share on other sites

Shot in the dark, but is '</html>' rendered in 'InputfieldTextarea::render'? I know it's rendered in 'Page::render'. Might be worth taking a look at what $event->return is actually outputting. Maybe just echo $event->return. 

  • Like 1

Share this post


Link to post
Share on other sites

I think Tom is right. Inputfield render is the inputfield markup not the whole <html>.

Why not just use the core scripts array?

$this->wire("config")->scripts->add("/xxx/xxx.js");

  • Like 1

Share this post


Link to post
Share on other sites
11 minutes ago, Soma said:

Why not just use the core scripts array?

$this->wire("config")->scripts->add("/xxx/xxx.js");

jep, just take care when using AJAX loaded fields. if you add your JS via a ::render hook it will not get loaded. You need to load it via ::renderready

 

 

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, Soma said:

$this->wire("config")->scripts->add("/xxx/xxx.js");

oops, of course. Works like a charm now. .

 

1 hour ago, bernhard said:

::renderready

Thanks for the heads-up. Will keep it in mind.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VeiJari
      Hello forum!
      I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 
      Now here's the code for the hook:
      wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
        $page = $hook->arguments(0);
        $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
        wire()->log->message($page->name);
        $page->setAndSave('name', $newUrl);
      });
      I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 
      Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php
      and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
       
      What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.
      Thanks for the help!
    • By VeiJari
      Hello forum! I'm trying to learn how to time up my functions by using lazycron but I can't get it fired up. 
      Here's my code in site/init.php:
      function setEventToRepeat(HookEvent $e) { $wire->log->message("kutsutaan cron"); $events = $wire->pages->find('template=tapahtuma'); die(var_dump($events)); $wire->log->message('Kaikki tapahtumat haettu:' .$events); }   $wire->addHook('LazyCron::every30Seconds', null, 'setEventToRepeat'); I get nothing in the logs and can't echo or dump anything
      Thanks for the help!
    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
    • By celfred
      Hello,
      I am getting nuts trying to understand hooks and I hope someone in the community will be able to help. This is deiving me crazy ! I have tested tens of possibilities to eventually reduce my code to this :
        bd('outside');   $wire->addHookAfter('Page::render', function($event) {     bd('inside');   }); And if someone could tell me why my bd('inside'); never triggers... I would be infinitely grateful !
      EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)
    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
       
      Thanks in advance !
×
×
  • Create New...