Zeka Posted October 6, 2017 Share Posted October 6, 2017 Hi! Is there some helper method that can extract fields values to an array like it do WireArray::explode()? $data = $page->explode(["title", "desc", "name"]); /// [ "title" => "value", "desc" => "value", "name" => "value", ] Link to comment Share on other sites More sharing options...
abdus Posted October 6, 2017 Share Posted October 6, 2017 No but you can use a hook like this: wire()->addHookMethod('Page::explode', function (HookEvent $e) { $arr = new WireArray(); $arr->add($e->object); $e->return = $arr->explode($e->arguments(0))[0]; // ONE LINER // $e->return = (new WireArray)->add($e->object)->explode($e->arguments(0))[0]; }); 4 Link to comment Share on other sites More sharing options...
adrian Posted October 6, 2017 Share Posted October 6, 2017 Nice one @abdus ! Just a quick tip for others. You can also test building hooks directly in the Console panel. 3 Link to comment Share on other sites More sharing options...
abdus Posted October 6, 2017 Share Posted October 6, 2017 Yup. That's a really nice feature of hooks. You can use it anywhere after the point you set it. Quote Where should I define my hook? Hooks are most often defined in ProcessWire plugin modules or initialization template files. However, this is not a requirement, and you may technically define hooks anywhere you want. But you need to make sure that your hook really does get called when you want it to, so your hook must be defined and attached sometime before the method you are hooking gets called. https://processwire.com/api/hooks/#where_define Link to comment Share on other sites More sharing options...
Robin S Posted October 6, 2017 Share Posted October 6, 2017 Not what was asked, but if you ever have a need to get all the field values and properties of the page in an array you can do this: $data = (array) $page->getIterator(); 3 Link to comment Share on other sites More sharing options...
abdus Posted October 6, 2017 Share Posted October 6, 2017 Nice one @Robin S, I've never seen that one used anywhere before. It seems to fetch all fields before handing you an ArrayObject. public function getIterator() { $a = $this->settings; if($this->template && $this->template->fieldgroup) { foreach($this->template->fieldgroup as $field) { $a[$field->name] = $this->get($field->name); } } return new \ArrayObject($a); } One thing that I'd like to have with explode() method is to get deeper fields with dot notation, such as $thumbData = $page->explode(['url', 'title', 'images.first.url', 'tags.count']); like underscore.php (or understore.js) does. I guess I should start building a module. 5 Link to comment Share on other sites More sharing options...
adrian Posted October 6, 2017 Share Posted October 6, 2017 7 minutes ago, Robin S said: Not what was asked, but if you ever have a need to get all the field values and properties of the page in an array you can do this: $data = (array) $page->getIterator(); Or check out the "Field List & Values" section of the PW Info panel in Tracy, which takes things a step further and explodes values which returns arrays (like images) and provides other details about the fields: 3 Link to comment Share on other sites More sharing options...
Robin S Posted October 6, 2017 Share Posted October 6, 2017 11 minutes ago, abdus said: I guess I should start building a module. Please 3 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now