Jump to content

How to I get values of a page by a field tag?


Alpine418
 Share

Recommended Posts

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

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;
}

 

  • Like 1
  • Thanks 2
Link to comment
Share on other sites

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').

 

 

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
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);
		
		}

 

  • Like 1
Link to comment
Share on other sites

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);
}

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...