Search the Community

Showing results for tags 'hook'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • Welcome to ProcessWire
    • News & Announcements
    • Showcase
    • Wishlist & Roadmap
  • Community Support
    • Getting Started
    • Tutorials
    • FAQs
    • General Support
    • API & Templates
    • Modules/Plugins
    • Themes and Profiles
    • Multi-Language Support
    • Security
    • Jobs
  • Off Topic
    • Pub
    • Dev Talk

Found 49 results

  1. I think you can create a simple logout link with <a href="admin/login/logout">log out</a> But it redirects to the login form in the admin area. Can I hook into logout and change the redirect? Default redirect home would make sense. Or is the way to do this still to create a logout template + page with session->logout() and a redirect?
  2. Hi PW fanatics In this post I share two of my addHookMethods for those interested. As you surely already know (if not, time to take a look at it) the Wire::addHookMethod() and the Wire::addHookProperty() API methods can be used to "breath some extra OOP" into your projects. Using addHookMethods and addHookProperty are alternatives to "Using custom page types in ProcessWire". The methods I want to share: basically the idea here is to get the URL pointing to images uploaded in the admin without writing much code in the template files. With methods like these below, it does not matter what Formatted value is set to the images, because some predefined defaults are used in all circumstances. #1 Example template file code: <?php $img_src = $latest_article->siteFeaturedImage(); ?> <img src="<?= $img_src ?>" alt="<?= $page->title ?>"> addHookMethod goes into /site/init.php <?php /* Returns URL of the original Pageimage of Article. * Image field's value can be either null (default missing image), Pageimage or Pageimages. * * @return string */ $wire->addHookMethod('Page::siteFeaturedImage', function($event) { $page = $event->object; if ($page->template != "article") { throw new WireException("Page::siteFeaturedImage() only works on 'Pages of article template', Page ID=$page is not such!"); } $article_featured = $page->getUnformatted('article_featured'); //always a Pageimages array if (count($article_featured)) { $img_url = $article_featured->first()->url; } else { $img_url = urls()->templates . "assets/img/missing-article_image.jpg"; //we show this when image is not available } $event->return = $img_url; }); ?> #2 Example template file code: <?php $img600_src = $page->siteProductImageMaxSize(600, 600, ['rotate' => 180]); ?> <img src="<?= $img600_src ?>" alt="<?= $page->title ?>"> addHookMethod goes into /site/init.php <?php /* Generates image variations for Product images. Returns URL of Pageimage. * Image field's value can be either null (default missing image), Pageimage or Pageimages. * * @param int arguments[0] Max allowed width * @param int arguments[1] Max allowed height * @param array arguments[2] See `Pageimage::size()` method for options * @return string */ $wire->addHookMethod('Page::siteProductImageMaxSize', function($event) { $page = $event->object; if ($page->template != "product") { throw new WireException("Page::siteProductImageMaxSize() only works on 'Pages of product template', Page ID=$page is not such!"); } $width = isset($event->arguments[0]) ? $event->arguments[0] : 48; //default width $height = isset($event->arguments[1]) ? $event->arguments[1] : 48; //default height $options = isset($event->arguments[2]) ? $event->arguments[2] : $options = array(); //default empty options $product_image = $page->getUnformatted('product_image'); //always a Pageimages array if (count($product_image)) { $img_url = $product_image->first()->maxSize($width, $height, $options)->url; } else { $img_url = urls()->templates . "assets/img/product-missing-image.jpg"; //we show this when image is not available } $event->return = $img_url; }); ?> BTW, you can find more examples here: How can I add a new method via a hook? Working with custom utility hooks Adding array_chunk support to WireArray addHookProperty() versus addHookMethod() Have a nice weekend!
  3. I'm trying to add a new option to InputfieldTextarea. Depending on that option, I want to change how the input is rendered. I also want to change this option depending on different templates and repeaters, meaning it can have different values for different fieldgroups. I hooked into three methods: $this->addHookBefore('InputfieldTextarea::render', $this, 'hookInputRender'); $this->addHookAfter('InputfieldTextarea::getConfigInputfields', $this, 'hookInputSettings'); $this->addHookAfter('InputfieldTextarea::getConfigAllowContext', $this, 'hookInputContext'); In hookInputSettings, I build the additional option protected function hookInputSettings(HookEvent $e) { /** @var InputfieldTextarea $field */ $wrapper = $e->return; $field = $e->object; /** @var InputfieldSelect $font */ $font = $this->modules->get('InputfieldSelect'); $font->label = $this->_('Font'); $font->name = 'fontFamily'; $font->addOptions(self::fontOptions); $font->attr('value', $field->fontFamily); $wrapper->add($font); $e->return = $wrapper; } It shows up in field settings with no problem When I pick an option and save, it even shows up in the database. However, I cannot get the properties of that field in that fieldgroup context. Most other inputfields can get their inputfield settings because are inside a class that extends Inputfield, so $this->myOption works. If I hook into FieldtypeTextarea::getConfigInputfields, it works too, because getConfigInputfields method is called with $this as its argument, inside hooks it's possible to access fieldgroup specific settings. But for Inputfield, it's not given any arguments, so hooking Inputfield::getConfigInputfields, you won't be able to get any information about the context. // /wire/core/Field.php public function ___getConfigInputfields() { // ... if(!$fieldgroupContext || count($allowContext)) { // ... try { $fieldtypeInputfields = $this->type->getConfigInputfields($this); // ... } // ... } $inputfields = $this->wire(new InputfieldWrapper()); // ... if($inputfield) { if($fieldgroupContext) { $allowContext = array('visibility', 'collapsed', 'columnWidth', 'required', 'requiredIf', 'showIf'); $allowContext = array_merge($allowContext, $inputfield->getConfigAllowContext($this)); } // ... $inputfieldInputfields = $inputfield->getConfigInputfields(); // ... } // ... } So, as a solution I gave it $this as a parameter // $inputfieldInputfields = $inputfield->getConfigInputfields(); $inputfieldInputfields = $inputfield->getConfigInputfields($this); Now everything works without any hacks. It works in field settings, templates, repeaters just fine. protected function hookInputSettings(HookEvent $e) { // ... $field = $e->arguments(0); // ... $font->attr('value', $field->fontFamily); // WORKS! // ... } I wanted to write this post because it drove me mad last night. I guess the next step is to make a pull request.
  4. Hi community, i want to add a selector to every pages->find() method. I found this hook solution, and Ryan mentioned this somewhere in this forum. $this->addHookBefore('Pages::find', function(HookEvent $event) { // Get the object the event occurred on, if needed $pages = $event->object; // Get values of arguments sent to hook (and optionally modify them) $selector = $event->arguments(0); $options = $event->arguments(1); /* Your code here, perhaps modifying arguments */ // Populate back arguments (if you have modified them) $event->arguments(0, $selector); $event->arguments(1, $options); }); If i try to add something to the given $selector, the error occurs: Exception: Unknown Selector operator: '' -- was your selector value properly escaped? But the syntax is right, isn´t it? $this->addHookBefore('Pages::find', function(HookEvent $event) { // Get the object the event occurred on, if needed $pages = $event->object; // Get values of arguments sent to hook (and optionally modify them) $selector = $event->arguments(0); $options = $event->arguments(1); /* Your code here, perhaps modifying arguments */ $selector.= ", loremipsum_field!=1"; /* <----------------------- */ // Populate back arguments (if you have modified them) $event->arguments(0, $selector); $event->arguments(1, $options); }); What i am doing wrong? Thank you for your advice!
  5. Hello, I read about conditional hooks and wanted to utilize them. In the process of trying to implement them I found that they do not get called. So I reduced my hook to a minimum wire()->addHookAfter('Page::changed', function($event) { die('changed'); }); And nothing happens at all. Also with Page::changed(title) or Page(template=basic-page)::changed(title) no luck. In the code comment in Wire.php it says: "Hookable method that is called whenever a property has changed while change tracking is enabled" I see that change tracking is enabled as wire()->addHookAfter('Pages::saveReady', function($event) { if($event->arguments[0]->isChanged('title')) die('changed'); }); is working. The hookable method ___changed() in Wire.php is empty. I tried this on 2 different installs PW 3.0.61 and 3.0.62 Can anyone please confirm or let me know if I'm doing anything wrong here. Thank you.
  6. Hi everyone, after successfully solving the mystery "PageTable", I stumbled upon another one: removing the "add new" button from PageTable (in the template) via hooks. So, I read about hooks. I never implemented one myself, but tried using the "init" PHP file from the default site, but that didn't work out too well. So again I am a bit stuck in the process. Basically, I check if an array is empty. If it is, the PageTable field on my template shouldn't display the "add new" button. That's about it. Since PageTable is fairly new, I didn't find too much info about it. Maybe someone of you is able to help me? Thanks a lot!
  7. Hello! The only hook into 'trash' in a module runs twice, by itself, as it seems. The following code outputs: Session: Hooked 1 times on abcd Session: Hooked 2 times on abcd on the admin side when one page is deleted from it's delete tab. And it's no matter 'addHookBefore', 'addHookaAfter' or 'addHook' method is used. Why?! private $i = 0; public function init() { $this->pages->addHookBefore('trash', $this, 'test'); } public function test($event) { $page = $event->arguments('page'); $this->i++; $this->message("Hooked {$this->i} times on {$page->title}"); }
  8. So I discovered in this topic ( ) that I can cancel all hooks after my own hook. Now I want something like that. I want my hook which is called on page render to cancel all other events, also the after page render and the before page render methods, is this possible? This only need to be done when some conditions are met. /** method replaces original page::render method and is called with hook priority one */ method onPageRender (HookEvent $event) { if (my condition is true) { $this->cancelHooks = true; // to cancel all hooks to page::render // how to cancel after Page::render events // how to cancel before Page::render events } } Is this possible at all?
  9. I've written a hook which replaces the page::render method. Based on a few conditions, I might want to output something different from what page::render would return. So when my conditions aren't met, I want to use the return value of the original page::render method. How can I do this? At the moment I've got something like this: function pageRenderHookMethod (HookEvent $event) { if ('my condition' == true) { $event->replace = true; $event->return = 'my custom return value'; } else { $event->return = 'original return value of page::render'; } } But I don't know how to get the original return value of page::render because the would trigger my hooked method instead of the original method.
  10. I have created a hook with priority one because I want it to run asap. Now I want this same hook to prevent all other hooks from running, is this possible, if so, how can I achieve that? For example, normally hooks would run like this: hook 1 -> hook 2 -> hook 3 -> hook 4 -> etc. Now I want it like this: hook 1 -> hook 2 -> hook 3 -> hook 4 -> etc.
  11. Greetings to all processwire fans, I am new in processwire and I have used it since 3 weeks. Probably it's a really silly question but does someone now how I can execute a function in my own module, which is triggered when I save a pages, which contains a user-defined template. I tried to to this with an basic if/else condition to trigger the hook when the page has theTemplateName== "". Unfortunately it doesn't work. So I try this which might be the wrong Syntax. Perhaps someone can give me a hint THANKS a lot! public function init() { // add a hook after the $pages->save, to issue a notice every time a page is saved $this->page->template == "templateName"->addHookAfter('save', $this, 'example1'); }
  12. Hello, I am still learning ProcessWire. I am trying to hook a method in ListProConfig.php. After the processInput method is called I would like to know the user id of the person that called the method, the page name that was involved in changed lister settings, and then get the new settings. I am having problems getting my hook to work. In the init of my module I have the following code: wire()->addHook("ListerProConfig::processInput", function($event){ $this->wire('log')->save('stephen_ext',"Someone called processInput"); }); Even though I know that processInput is called I never get anything written to my log.
  13. How can I change the output of `Page::render`? I've created a hook before page::render, but when I set the $event->return to something else, my whole page is replaced with that value. For example: public function beforePageRender(HookEvent $event) { $event->return = 'blaat'; } This will cause my whole page to be nothing more than just 'blaat'. What I want, is that my menu, footer etc. are still visible but only the content of that page is replaced.
  14. I have a hook to change the template file on a certain page. This is not working at the moment for some reason. The usual location to place templates is under /site/template. Because the template should only be available in my module, I want my template file to be in /site/modules/mymodule/view/mytemplate.php When I place my file in the usual template location, it works fine. But when I place the file in my module folder, it doesn't work. When I var_dump the $page->template->filename variable, the location of the template file is correct and maps to my module folder. So everything seems to be correct, but somehow it's not working but I can't figure out the problem. Does anyone know how to solve this?
  15. Hi, I have a method (___renderImage) which renders an image tag (<img src) and has a single argument: Pageimage $image. Now I have a hook called "beforeRenderImage" in which I'd like to add a property to the argument of ___renderImage. So for example, I'd like to add a property "requiredDimension" to the argument before ___renderImage is called. Is this possible?
  16. I've added a hook after the '"ProcessPageEdit::buildFormContent" event. I've added a new config option to the language settings: public function beforeAdminProcessPageEditBuildFormContent (HookEvent $event) { $editPage = $this->getPage(wire('input')->get('id')); if ($editPage->template->name == 'language') { // only add field when editing language page $field = wire('modules')->get('InputfieldText'); $field->attr('name+id', 'locale'); $field->label = __('Locale'); $event->return->insertAfter($field, $event->return->getChildByName('title')); } } I've also created a field (Setup > Fields > Add new) which I've also named 'locale'. The field is displayed, but the value isn't saved when I click the save button. How do I make sure the input of the field is saved as well? And how can I access the stored value once it's saved?
  17. Hello, I am currently playing around to build my first module. It will be a simple module for the ImageOptim web service, but I am a little bit stuck on which right hook to use. Currently I use following hook: $this->pages->addHookAfter('saveFieldReady', $this, 'imageOptimSave'); I would like to pass images and its variations to the web service after adding them to the image field. But if I use the saveFieldReady hook only the original image will be optimized, not the API generated image variations, because they will be generated after viewing the page for the first time. Can anybody please help me how to hook into the image variations? I already tried to use my own method, but this one used in the template would be called on every page render. Regards, Andreas
  18. Hello, for quite a while I found it quite annoying that when you edit a page from a lister view and then save+exit you will end up at the page tree and not at the lister view where you came from. I haven't found a setting that lets you return to the lister view. I wrote a little hook that does just that for a project where I am using Lister Pro. The lister is called Events and has a fixed url. It lists only one type of pages with template event. Here's the working hook code that I placed in admin.php: // redirect to events list on save+exit wire()->addHookBefore('ProcessPageEdit::processSaveRedirect', function($event) { $id = (int) $this->input->post('id'); $page = wire('pages')->get($id); if($this->input->post('submit_save') == 'exit' && $page->template->name == 'event' && wire('user')->hasRole('member')) $this->session->redirect(wire('config')->urls->admin . 'events/'); }); Now I would like to make this more generic so that it works for any lister. How can I get the referring URL (other than from $_SERVER['HTTP_REFERER']) to use it instead of the hardcoded URL I have now in the redirect? And is there any way that I can access properties of the instance of ProcessPageEdit that called processSaveRedirect? The $event->object is the processSaveRedirect method and $this is ProcessWire. How can I access properties of ProcessPageEdit? E.g. $id or $page. ATM I am getting the page id from the $input->post. Obviously I am not a savvy PHP OOP dev and have no idea how to do such things in a proper way EDIT: I know I could open pages from the lister view in a modal to avoid having to redirect. But I don't want to use that option. $_SERVER['HTTP_REFERER'] gives me the URL of the page that I edited and not the URL of the lister.
  19. Hello, I'm trying to hide a specific field with a hook. This is what I tried so far. Changing the field label works but the hiding not. Changing addHookBefore to addHookAfter makes no difference. I searched the forum and found similar thread but could not translate the answer to this situation. Any help would be appreciated. class HideFields extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Hide Fields', 'version' => 100, 'summary' => 'Hide fields', 'singular' => true, 'autoload' => "template=admin" ); } public function init() { $this->addHookBefore('Inputfield::render', $this, 'setHide'); } public function setHide($event) { $inputfield = $event->object; if($inputfield->name != "Foo") return; $inputfield->label = "Bar"; // This works $inputfield->collapsed = Inputfield::collapsedHidden; // This doesn't work } }
  20. How would one pass a variable set in one module to another module via a Hook? e.g. module 1. public function ___saveUser($vars){ $userSaved = true } module 2 public function init() { $this->addHookAfter('module1::saveUser', $this, 'userSaved'); } public function userSaved($e){ $userSaved = $e->userSaved; return $userSaved; // true? }
  21. Hey, I made a hook that is triggered when a page with a certain template is saved. I meant to create the script only for the backend of the website. Now, whenever a page is saved on the front end (with the API) the module is being triggered. I was wondering if there is a method to have it only trigger whenever a page is saved on the backend (so not through the API). Jim
  22. Hi I have an image field in the back end, that expects multiple images. Now I've added this hook as the files are added: $page->addHookAfter('InputfieldFile::fileAdded', function($event) { $inputfield = $event->object; wire('log')->save('image-resizing', "Resizing "); $image = $event->argumentsByName("pagefile"); $image->size(800,0,array('upscaling' => false)); $image->size(300,200,array('cropping' => true, 'quality' => 80 )); }); It works absolutely fine for a small amount of images, but uploading a large amount doesn't seem to work. No errors are logged either. Now all thes photos appear to have uploaded in the back end, and show the thumbnails, but upon clicking save images are missing. I've checked all my php settings and ensured they are all adequate enough. memory_limit 512mb upload_max_filesize 200mb max_execution_time 300 If I disable this hook, I can upload all images fine. Please could anyone other any insight into what may be causing this issue? Thanks in advance
  23. Folks, I may have discovered a) a bug or b) that I can't write code for shit. I have this hook setup: $pages->addHookAfter('Pages::saveReady', function($event) { $page = $event->arguments(0); $repeater = $page->collections_detail_images; foreach ($repeater as $row) { $row->of(false); $row->collections_detail_image_page_title = $page->title; $row->collections_detail_image_page_type = $page->collections_detail_category->name; $row->collections_detail_image_page_pattern = $page->collections_detail_pattern->name; $row->collections_detail_image_page_designer = $page->collections_detail_designer->name; $row->setAndSave(); } }); For each repeater row on a page I am editing, upon save, it fills in the desired fields with content required. This all works well, however, the issue/bug is this... Each repeater row also has an image field (limited to 1), which I have been dragging and dropping an image to, for each row in the repeater, then saving. I tend to add 5-10+ rows (each with an image) at once then save. This is where the issue occurs. Even though it looks like the image is there and saved... it's not appearing on the front-end. If I do one row at a time and save, it works fine, it's only if I do 2 or more in one 'save session'. This only happens with drag and drop too. If I remove my hook, shown above, I don't seem to get any issue. Any thoughts?
  24. I'm curious to know if this is possible. I'd like to write a hook that upon adding an image to a specific page it will rename the image to the name of the page it's been/being uploaded to? I have a bank of images all with really useless filenames but would like to sort this upon uploading each one. Each image is added in a repeater with a corresponding colour (Pagefield), for example: I wonder if I could write a hook that would take each image in the repeater and rename each one something like pagetitle_01_charcoal pagetitle_02_red What do you think? Is it possible to write a hook to change a filename? Either on save or as it's uploading? Any advice/help is appreciated.
  25. Hi everybody, im planning to release an updated version of the TwigReplace module for ProcessWire 3. In addition to the known config options for setting up the twig environment, I'm wantg to provide the means to enable twig extensions at will. But this can't be done via module config options because of different reasons (finding the correct twig extenion classes - installed or self-made, providing constructor arguments to the twig extension, ...). To solve this I plan to add a hookable method to my module that instantiates and returns the Twig_Environment object. If this works, users of the module may create their own project specific modules to intercept this hook and modify the Twig_Environment instance by adding twig extensions at will. Here's the skeleton of my module (with ___initTwig() being the hookable method): namespace nw\ProcessWire3; use ProcessWire\FileCompilerModule; class FileCompilerTwig extends FileCompilerModule { /** * @var \Twig_Environment */ protected $twig; /** * Creates the twig render engine if necessary */ public function init() { /* * gather module config settings ... */ $this->twig = $this->initTwig($pwTemplatesPath, $twigOptions); } /** * Instantiates a Twig environment * * THIS IS THE HOOKABLE METHOD. * * @param string $pwTemplatesPath * @param array $twigOptions * @return \Twig_Environment */ public function ___initTwig($pwTemplatesPath, array $twigOptions) { $loader = new \Twig_Loader_Filesystem($pwTemplatesPath); return new \Twig_Environment($loader, $twigOptions); } This works fine so far. But when I try to hook into the ___initTwig method via another module, nothing happens. Now I'm not sure if the entire concept (hooking into a module from another module) isn't supported by ProcessWire, or if I just haven't found the correct way to address the desired hook (maybe due to namespacing issues). So far I tried: namespace nw\ProcessWire3; use ProcessWire\HookEvent; use ProcessWire\Module; use ProcessWire\WireData; class FileCompilerTwigExtensions extends WireData implements Module { public function init() { $this->addHookAfter('FileCompilerTwig::initTwig', $this, 'addTwigExtensions'); // didn't work either // $this->addHookAfter('nw\ProcessWire3\FileCompilerTwig::initTwig', $this, 'addTwigExtensions'); // $this->addHookAfter('\nw\ProcessWire3\FileCompilerTwig::initTwig', $this, 'addTwigExtensions'); } public function addTwigExtensions(HookEvent $event) { /** @var \Twig_Environment $twigEnv */ $twigEnv = $event->return; $twigEnv->addExtension(new \Twig_Extension_Debug()); } } I'm thankful for any suggestions. wumbo