Alpine418 Posted July 3, 2019 Share Posted July 3, 2019 Hi, how do I get the values of a page tagged by specific field tag? Something like $page->getByTag('custom'), if I had tagged multiple fields by a tag called "custom". Thanks, rjgamer Link to comment Share on other sites More sharing options...
horst Posted July 3, 2019 Share Posted July 3, 2019 You would ask with $pages->get(„yourfieldname=requiredvalue“), or with $pages->find() If you not allready have setup your tags field, you may look at the pagereference fieldtype, or the options fieldtype. Your selector to find pages with the requested flag may include specified templates, parent or others, to be more precise. And if you not allready know the differences between get and find, look it up in the docs, as this is fundamental knowledge in PW. Link to comment Share on other sites More sharing options...
Robin S Posted July 3, 2019 Share Posted July 3, 2019 Try: // Get names of fields on the page that are tagged with "custom" $custom_field_names = $page->fields->find('tags=custom')->explode('name'); // Do something with the values of those fields foreach($custom_field_names as $custom_field_name) { echo $page->$custom_field_name; } 1 2 Link to comment Share on other sites More sharing options...
Alpine418 Posted July 3, 2019 Author Share Posted July 3, 2019 21 minutes ago, Robin S said: Try: // Get names of fields on the page that are tagged with "custom" $custom_field_names = $page->fields->find('tags=custom')->explode('name'); // Do something with the values of those fields foreach($custom_field_names as $custom_field_name) { echo $page->$custom_field_name; } Thanks! I already found $page->fields in the docs, but didn't reconise that I could use ->find('tags=custom'). 1 Link to comment Share on other sites More sharing options...
Alpine418 Posted July 3, 2019 Author Share Posted July 3, 2019 56 minutes ago, horst said: You would ask with $pages->get(„yourfieldname=requiredvalue“), or with $pages->find() If you not allready have setup your tags field, you may look at the pagereference fieldtype, or the options fieldtype. Your selector to find pages with the requested flag may include specified templates, parent or others, to be more precise. And if you not allready know the differences between get and find, look it up in the docs, as this is fundamental knowledge in PW. Sorry, I think you misunderstood my question. I asked for a solution to find field values of a page, based on a specific field tag and not pages. 1 Link to comment Share on other sites More sharing options...
Chris Bennett Posted July 17, 2020 Share Posted July 17, 2020 On 7/3/2019 at 9:57 PM, Robin S said: Try: // Get names of fields on the page that are tagged with "custom" $custom_field_names = $page->fields->find('tags=custom')->explode('name'); // Do something with the values of those fields foreach($custom_field_names as $custom_field_name) { echo $page->$custom_field_name; } Thank you Robin S, I find myself continually (eventually) finding the answers I am looking for with your name attached. Many thanks! Had gone round and round using the wrong method for the job I wanted. In my case, was just trying to clean up fields added by a module generated page I was playing with. Deleting api generated page, templates and fieldgroups was very easy but kept on bumping up against deleting the fields. Finally realised I could just set a tag on them, but kept using the (very wrong) getTags() method instead of the delightfully easy find('tags=...). Knew I was doing something wrong and when I eventually found your advice was acting as expected in minutes. public function ___uninstall() { parent::___uninstall(); $page = $this->pages->get('name='.self::PAGE_NAME); if ($page->id) { $page->delete(); } $addedTemplate = $this->templates->get(MODULE_NAME); if ($addedTemplate) { $this->templates->delete($addedTemplate); } $addedFieldgroup = $this->fieldgroups->get(MODULE_NAME.'-fieldgroup'); if ($addedFieldgroup) { $this->fieldgroups->delete($addedFieldgroup); } $addedFields = $this->wire->fields->find('tags='.MODULE_NAME.''); foreach($addedFields as $addedField) { $field = $this->wire->fields->get($addedField); $this->wire->fields->delete($field); } 1 Link to comment Share on other sites More sharing options...
Robin S Posted July 17, 2020 Share Posted July 17, 2020 45 minutes ago, Chris Bennett said: $addedFields = $this->wire->fields->find('tags='.MODULE_NAME.''); foreach($addedFields as $addedField) { $field = $this->wire->fields->get($addedField); $this->wire->fields->delete($field); } In this part, each $addedField is already a Field object so you could simplify it to: $addedFields = $this->wire->fields->find('tags='.MODULE_NAME.''); foreach($addedFields as $addedField) { $this->wire->fields->delete($addedField); } 2 1 Link to comment Share on other sites More sharing options...
Chris Bennett Posted July 17, 2020 Share Posted July 17, 2020 See, more thanks needed :) Love your work Robin 3 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