Jump to content

Juergen

Members
  • Posts

    1,427
  • Joined

  • Last visited

  • Days Won

    18

Everything posted by Juergen

  1. Great! I didnt know that this exist
  2. In my case I want to count the children which are under the current page beeing edited. Each child page is a date, so I count of how many dates this page has. But counting the children via siblings could also be an opportunity.
  3. Here is my code for others who also deals with the same issue. This little function prevents the deletion of the last child page if page has a certain template. Code runs inside ready.php. //prevent deletion of last child function wire()->addHookBefore('Pages::trash', null, 'preventdeletionlastevent'); wire()->addHookBefore('Pages::delete', null, 'preventdeletionlastevent'); function preventdeletionlastevent($event) { $page = $event->arguments(0); if(!in_array($page->template->name, ['single-date', 'single-event', 'single-business-vacation', 'single-special-business-hours'])) return; $parent = $page->parent; $childrennumber = count($page->parent->children); if($childrennumber === 1) { $event->replace = true; // now original function won't be called $event->return = wire()->warning(__("Deleting of the last date is not allowed. There must be at least 1 date.")); } else { wire()->message(__("1 date was deleted.")); } }
  4. Aaahh, I see!! This would be a solution Thanks!!!
  5. Thanks @Zeka, but I guess in this case I have to define the functions for each "Class::method" - so I have also 2 functions ("Pages::trash", "Pages::delete") - so I can leave the 2 hooks as they are.
  6. Hello @ all, In my case I have some hooks with the same code which run on page "Pages::trash" and "Pages::delete" inside may ready.php. For the moment I have used separate hooks for each of them. Is there a possibility to combine them so the same code runs only once and includes "trash" and "delete"? Explanation: Instead of $wire->addHookBefore("Pages::trash", function($event) { .... run code }); and $wire->addHookBefore("Pages::delete", function($event) { .... run code }); combine them $wire->addHookBefore("Pages::trash","Pages::delete", function($event) { .... run code }); This would be more efficiently for further changes and reduces the code. Best regards
  7. Hello @kongondo, I have discovered a strange behavior: I use a pagetable field inside a parent page. If click on the link inside the pagetable to open a child page inside a modal, I will be logged out from the system if the child page has a matrix field inside. I have traced down the problem and it has to do with a hook that I run on the pagetable field of the parent page (inside my ready.php): $wire->addHookBefore('InputfieldPageTable::render', function($event) { if($this->process != 'ProcessPageEdit') return; $table = $event->object; $page = $this->process->getPage(); // event table if(in_array($table->name, ['datespagetable', 'specialbusinesshourstable', 'eventspagetable', 'businessvacationpagetable'])) { if(count($page->children) == 0){ $table->notes = ""; $this->buttonHook = $this->addHookAfter("InputfieldButton::render", null, function(HookEvent $event){ $event->return = __("Here all dates will be shown afterwards if some were created."); }); } //this part of the button hook is responsible for loggin me out if I click on the link to the page inside the pagetable else { $this->addHookBefore("InputfieldButton::render", null, function(HookEvent $event){ $button = $event->object; if($button->name == 'button'){ $button->attr('value', __('Add event')); } }); } } }); Inside this hook there is another hook for rendering the "Add new" button. So this "InputfieldButton::render" hook is responsible for loggin me out (if I remove this part everthing is fine). So its not a big problem for me (I have comment it out), but maybe you have an idea, why these lines of code could lead to this behavior in combination with the matrix field. Best regards
  8. Oh, sorry - my fault. I use the 2D repeater matrix from Kongondo.
  9. Hello @kongondo only a question: is it possible to use a special field of a page for the field of the matrix (fe a date field, an integer field,..)?
  10. Cannot install the module There is always the message that FieldtypRepeaterMatrix is not the correct version, but I have downloaded it today from the modules directory - so it must be ok.
  11. Here is a new created version to track changes which works without any problems and you dont have to take care about the deletion of input values if the page was not saved successfully (like in the version before) Put this little piece of code inside your ready.php. //Compare before and after values and output a warning message $pages->addHookAfter('Pages::saveReady', function($event) { $page = $event->arguments('page'); $page->of(false); //configuration: change it to your needs $templates = ['event_businessvacations', 'event_dates', 'event_events', 'event_specialbusinesshours']; //array of templates where this hook should run $fields = ['summary', 'body']; //array of fields which should be checked //configuration end if(in_array($page->template->name, $templates)){ $changedfields = []; foreach($fields as $fieldname){ if ($page->isChanged($fieldname)) { // Page as it is in the DB $oldPage = wire('pages')->getById($page->id, array( 'cache' => false, // don't let it write to cache 'getFromCache' => false, // don't let it read from cache 'getOne' => true, // return a Page instead of a PageArray )); $changedfields[] = $oldPage->fields->$fieldname->label; } } $changedfields = implode(", ", $changedfields); if(!empty($changedfields)){ $this->warning(__("The following fields have been changed: {$changedfields}")); } } }); Change the configuration block to your needs (template names, field names). This little code snippet outputs only a warning message which fields have been changed - not more or less, but you can also run some other logics - its up to you. Note: Works also with repeaterfields, but you can only check the repeaterfields for changes in general. It is not possible to check for specific fields inside the repeater.
  12. Only to mention: after page save was not an opportunity for me, because the validation of the required field kicks in before this hook . I have tried it before, but thanks for your hints @Zeka
  13. I make the field non required - it is the most simple way
  14. Ok, I thought that the form data could be changed before processing the form if I use "$wire->addHookBefore("InputfieldDatetime::processInput", function($event) {" Thanks @BitPoet
  15. Hello @ all, I have a required field inside a template and I want to add a value via a hook if the user enters nothing in this field. Therefore I have tried to hook before "processInput". $wire->addHookBefore("InputfieldDatetime::processInput", function($event) { $page= $this->process->getPage(); $page->of(false); if(!in_array($page->template->name, ['single-event'])) return; if($page->parent->reservationdeadline != ''){ $diff = ($page->parent->starteventdate) - ($page->parent->reservationdeadline); if(($page->starteventdate) && ($page->reservationdeadline != '')){ $page->reservationdeadline = ($page->starteventdate)-$diff; } } }); So if the user presses the save button, then it should be checked if a value was entered in date field called "reservationdeadline". If not then a value should be entered via the hook before the validation of the required field takes place. At the moment I always get the error message that there is no value entered in the required field. Could someone give me a hint how to achive this? I know I could set the option to non required and enter the value always if nothing is there but it is possible to change it before the form will be validated?
  16. Thanks I am always glad to read about optimizations!
  17. Oh I understand! I only check after starting the hook if it is the right template and then make some manipulations. So it would be better to check before starting the hook if it is the right template. Thats the current case: $pages->addHookAfter('saveReady', function($event) { $page = $event->arguments(0); $page->of(false); $pages = wire('pages'); $name = $page->template->name; if(in_array($page->template->name, ['event_businessvacations', 'event_dates', 'event_events', 'event_specialbusinesshours'])){ ........ } }); But this would prevent the hook to be triggered on every page: $pageid = $input->get('id'); if(in_array(wire('pages')->get($pageid)->template->name, ['event_businessvacations', 'event_dates', 'event_events', 'event_specialbusinesshours'])){ $pages->addHookAfter('saveReady', function($event) { $page = $event->arguments(0); $page->of(false); $pages = wire('pages'); $name = $page->template->name; ........ }); } Is that what you mean?
  18. I dont know what you mean with endless recursion. The pages will created from an array with dates (fe an array with 10 dates). So 10 child pages will be created. There is a hard limit of 732 pages that can be created (limited by the RRULE library I use to get the recurrences of dates). Ah you mean recursion functions - translation problem. By the way: latest DEV and PHP 7
  19. In my case they dont work if I use special characters. I have also tested it with and without special characters and nothing will be shown if I use "ü", but if I replace the "ü" with "ue" everything works as expected. I also have replaced the hard coded message text with translateable text, so there are no German Umlauts present any more. But this is really a strange thing. The main problem is that I dont get any information from the log files or from Tracy, so it was a trial and error experiment. I removed always a little part of the code to check where the problem could be. This leads me to this cause.
  20. Yes I have done it with switch/case outside of the foreach - this was a part from the old code.
  21. OK, here is the final solution for this problem: Dont use German Umlauts (ä,ü,ö) inside your message texts. It prevents the message from appearing. Maybe this could be also the case with other special characters (fe ß) - but not tested. This doesnt work: $this->message("Überraschung"); but this works: $this->message("Ueberraschung"); A little mistake but really hard to find. So keep an eye to only use default letters in message texts!!!!!!
  22. Ok, I have traced down the problem once more and it seems that the creation and saving of child pages inside the hook function is responsible for this behaviour. If I comment out the creation of the child pages the messages appear: foreach($eventdatesarray as $eventdate){ if($page->template == 'event_businessvacations'){ $itemtemplate = 'single-business-vacation'; } if($page->template == 'event_dates'){ $itemtemplate = 'single-date'; } if($page->template == 'event_events'){ $itemtemplate = 'single-event'; } if($page->template == 'event_specialbusinesshours'){ $itemtemplate = 'single-special-business-hours'; } $k = new Page(); $k->of(false); $k->parent = wire('pages')->get($page->id); $k->template = $itemtemplate; // set template ......... ......... $k->save(); //especially the saving seems to make problems with messages } Does anyone have the same problem? The hook itself works and the child pages will be created, only the messages dont appear.
  23. No I have got no errors, wheter in PW logs neither in Tracy. But now the problem is back and it seems there is an interference with other hooks. The problem is that the hooks works well, but the messages still dont appear and I havent find a way to check where the problem comes from. Could the problem occur if you are running the same hook type multiple times ? In my case I have a lot of "addHookAfter('saveReady'...) hooks running.
  24. Ok I found the mistake: There was a missing $child->of(false); before saving values to a child page in a foreach loop and this causes the messages to not appear. So keep an eye to always include the command for unformatted values before storing something in the DB if you save!! Otherwise tracing the problem to its source will take a lot of time
  25. Could be the problem. Thanks for confirming the correctness of the code, so I can focus on finding the problem.
×
×
  • Create New...