Jump to content

gyo

Members
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

4 Neutral

About gyo

  • Rank
    Newbie
  1. Hey sforsman! Of course it's a nice thing that PW core uses it's own architecture to provide functionalities, and the whole "modular" idea it's really a big selling point. The modular admin is just awesome, it's like anything is possible. Which I love. I'll have to dive more into hooks and get better used to it, because that's where the extensibility power really lies. In any case thanks everybody for the precious help
  2. @Soma The idea is not to just format values but to modify (filter) a variable, whatever that might be. I understand the confusion, because the hook in the init method we were discussing actually restricts the plugin usage to fields. Just brainstorming. The real difference between using the Filter module and standard hooks is that the first was meant to run "on demand" (only when the function applyFilter() is executed), while the second is attached to any possible field that is output, even if you don't need it. About the naming convention, yeah, the word "filter" is some heritage from the WP concepts, but for the modules names, whenever is possible and if they don't collide, I personally prefer them short and single-worded. And yeah2 those helper functions might pollute the global namespace, they're probably not necessary as one could just call wire('Filter')->add(...); Anyways, I believe that to use hooks would be fine, just a little more verbose but they would just work: $this->addHookAfter("Fieldtype::formatValue", $this, function(HookEvent $event){ $field = $event->arguments("field"); if ($field->name = 'myfield') { $value = $event->return; $value = $event->arguments("value"); $page = $event->arguments("page"); $event->return = "new value"; } }); With Filter it would be: wire('Filter')->add('formatValue_myfield', function($value){ return 'new value'; }); But it only makes sense if somewhere else that value is filtered (like the init method of the Filter module, or inside template files etc...): $value = wire('Filter')->apply('formatValue_myfield', $value); I think I can go with the standard hooks even if it takes more code because I prefer to use core features, my only concern is: can we add many hooks without excessive overhead, for example due to the huge size of the $event object? Thanks for the feedback!
  3. Hi sforsman, actually that's the very reason I made the plugin for: fields. It's useful to format the fields values, because you don't always need the same exact value coming from the database. For instance the Select fields: since the keys get stored, you could retrieve their corresponding labels by using a filter. Will FieldType::formatValue be attached to any field? That gave me an idea... public function init() { $this->addHookAfter("FieldType::formatValue", function(HookEvent $e) { $page = $e->arguments(0); $field = $e->arguments(1); $value = $e->arguments(2); // Apply filter for any field $value = applyFilter('formatValue', $value); // Apply filter for a specific field type $value = applyFilter('formatValue_'.$field->type, $value); // Example: formatValue_FieldtypeText // Apply filter for a specific field name $value = applyFilter('formatValue_'.$field->name, $value); // Example: formatValue_myfield // Finally return the value $e->return = $value; }); } I'm just thinking if that's flexible enough. Surely you can remove the hook at runtime if it's not needed or there could be 2 more methods in the plugin to remove/get the filters. So – to disable the filters temporarily – you could: store the current filters, remove them, do whatever needs to go "unfiltered", and add them back if necessary. If you think the Filter plugin is useful I'll definitely expand it, and thanks for the feedback!
  4. Hello everyone, After playing a week with ProcessWire I'm really loving it. As a first project I wanted to create a Profile module based on the existing FrontendUserProfile trying to make it suit my needs, and I found the need for a filter system like the one WordPress has. I couldn't find anything similar or maybe I didn't completely grasp the Hooks concept, so I've created a simple ProcessWire module to handle that: Filter https://github.com/gyopiazza/pw.filter The module is autoloaded, and once installed you can use the 2 functions provided to filter data. This feature is very useful in certain situations where you want to be able to change some values in a simple way. function my_function1($value) { return 'filtered by my_function1'; } function my_function2($value) { return 'filtered by my_function2'; } // Parameters: filter name, function name or array(class, method), priority // Higher priority means later execution, default is 10 addFilter('some_filter_name', 'my_function1', 10); addFilter('some_filter_name', 'my_function2', 10); $result = 'default value'; $result = applyFilter('some_filter_name', $result); // $result = 'filtered by my_function2' Hope you like it, cheers! Giordano
×
×
  • Create New...