Jump to content
martind

Hooking buildFormContent() - set field parent_id to InputfieldPageAutocomplete

Recommended Posts

hi,

is it possible to change field parent_id from InputfieldPageListSelect to InputfieldPageAutocomplet by addHookAfter('ProcessPageEdit::buildFormContent')?

thanks,
martin

 

Share this post


Link to post
Share on other sites

It should be possible. You can use the methods of InputfieldWrapper on the return value of buildFormContent, retrieve the field by name, create the new Inputfield and insert that in the place of the original one with insertBefore()/insertAfter() and remove().

//...
$wrap = $event->return;
$origFld = $wrap->get('parent_id');

$newFld = wire('modules')->get('InputfieldPageAutocomplete');
$newFld->attr('name', 'parent_id');
$newFld->attr('value', $origFld->attr('value'));
// do any other instantiation work, like setting/copying title, label etc.

$origFld->parent()->insertAfter($newFld, $origFld)->remove($origFld);
// done

 

  • Like 3

Share this post


Link to post
Share on other sites

thank you. the problem i see is, that

$origFld = $wrap->get('parent_id');

is NULL. parent_id doesn't seem to be in $event->return
maybe this has to do with my module the hook lives in?

    public function ready() {
        $this->addHookAfter('ProcessPageEdit::buildFormContent', $this, 'hookAfter_ProcessPageEdit_buildFormContent');

		....

    public function hookAfter_ProcessPageEdit_buildFormContent($event) {
        $p = $event->object->getPage();
        $form = $event->return;

		....

		$origFld = $form->get('parent_id'); // and here the NULL

 

Share this post


Link to post
Share on other sites
7 hours ago, martind said:

parent_id doesn't seem to be in $event->return

Try hooking after ProcessPageEdit::buildForm. ProcessPageEdit::buildFormContent only contains the page template fields and not the fields from the Children, Settings, etc tabs.

Another hurdle is that the value of InputfieldPageAutocomplete is always an array, and ProcessPageEdit expects the value of the parent_id field to be an integer.

This seems to be working:

$wire->addHookAfter('ProcessPageEdit::buildForm', function(HookEvent $event) {
	/* @var InputfieldWrapper $form */
	$form = $event->return;
	$orig_pi_field = $form->getChildByName('parent_id');
	$attrs = [
		'collapsed',
		'required',
		'label',
		'icon',
		'id',
		'name',
		'value',
	];
	$new_pi_field = $event->wire('modules')->get('InputfieldPageAutocomplete');
	$form->insertAfter($new_pi_field, $orig_pi_field);
	$form->remove($orig_pi_field);
	foreach($attrs as $attr) {
		$new_pi_field->$attr = $orig_pi_field->$attr;
	}
	$new_pi_field->maxSelectedItems = 1;
});

$wire->addHookAfter('ProcessPageEdit::processInput', function(HookEvent $event) {
	/* @var InputfieldWrapper $form */
	$form = $event->arguments(0);
	if($form->id !== 'ProcessPageEditSettings') return;
	$pi_field = $form->getChildByName('parent_id');
	if(!$pi_field || !$pi_field->isChanged()) return;
	$parent_id = $pi_field->value;
	$parent_id = (int) reset($parent_id);
	/* @var Page $page */
	$page = $event->object->getPage();
	$page->parent_id = $parent_id;
});

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Robin, you're my hero. this works definitely out of the box. thank you.

  • Like 1

Share this post


Link to post
Share on other sites

Hey @martind, just a heads-up that I've moved this topic to the General Support area of the forum. Modules/Plugins area is only intended for module-specific dedicated support threads (for third party modules), and this question is about a core feature 🙂

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 Hardoman
      Hello community,
      we have a website running version 3.0.118. The owner would like to have a watermark merged to the images, that are being uploaded in the CKEditor as a requirement.
      Image upload besides the CKEditor within galleries and single images works as a charm already. We also use croppable image 3 there. (PIM2)
      To realize this requirement, I thought of using a hook in the admin area. So, I read a lot in our forums and tested this by adding a hook into the ready.php file.
      $this->addHookAfter('InputfieldFile::fileAdded',function(HookEvent$event){ wire('log')->save('test','Image upload works'); ... The log entry is being created correctly. But when I try to use the pim/watermark-function like in a template, he cannot find the watermark-image anymore. Furthermore, when I try to get the page-id, it does not seem to be accessible, because the application does not seem to know how to reference it, or I dont know the right way to do so…
      So my questions are:
       
      Is this the right attempt at all or will there be another, better workaround? It seems, I cannot access the page object (of the content page) within this scope or file but I would need it to save the processed image inside the right files/id folder Would it be better to place the hook into the admin-template? (or admin.php)
        Thanks for any hints in advance. 🙂
    • By Noel Boss
      👋 PW Pros…
      I have some hooks that I need to bind at the init phase (or even __construct) and I was wondering, and I couldn't find a good and simple way to determine if I'm in the admin. Would be nice if there is a reliable short option to do so, but I can't seem to find one… Is there a coherent way to tell this no matter where I am?
      Right now, I use the following method inside one of my modules:
      public function isAdmin($page = null) { if ( strpos($this->input->url, $this->urls->admin) !== false || $this->process instanceof ProcessPageList || $this->process instanceof ProcessPageEdit || ($page instanceof Page && $page->rootParent->id == $this->config->adminRootPageID) ) { return true; } return false; } @ryan wouldn't it be nice to have something like wire()->isAdmin(); like wire()->user->isLoggedin(); to tell if we are in admin – very early on (probably even in __construct() phase of modules?
    • 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 louisstephens
      So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?
      So after rereading my post, I believe it is a bit vague so I will try to explain more. 
      The Goal:
      Create a page with a template "one". Once the page is created/saved => create a new child page with the template of "two" If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page) The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.
×
×
  • Create New...