Jump to content

Checking if a page has a FieldtypeMulti field


gRegor
 Share

Recommended Posts

As part of the webmention module I am working on, I want to detect if the page being viewed has the "Webmentions" field and if so, add a Link: header. I have hooked before PageRender::renderPage to add the header and that works OK.

I am having trouble checking for the Webmentions Fieldtype, though. FieldtypeWebmentions extends FieldtypeMulti and is empty on new pages. My code below does not work because the $field->type is returning NULL when the Webmentions field is empty. When the Webmentions field is not null, then it returns the Fieldtype as expected.

How can I reliably check if a page's template has a specific Fieldtype, whether the field is empty or not? Am I missing something in the Fieldtype or Inputfield classes for the module? Thanks for any help!

private function findFieldtypeWebmentions(Page $page)
{

	# loop: each field for this page
	foreach ( $page->fields as $field )
	{

		if ( $field->type instanceof FieldtypeWebmentions )
		{
			$this->webmention_field = $field;
			return TRUE;
		}

	} # end loop

	return FALSE;
} # end method findFieldtypeWebmentions()
Link to comment
Share on other sites

I am not going to be much help, other than to say that your code works fine with empty Page fields, File fields, etc - ie other field types that extend FieldtypeMulti.

Can you can confirm the same at your end? If so, then it suggests it must be something specific to your FieldtypeWebmentions.

Can't see why it would make any difference at all, but have you tried iterating through the page's template?

foreach($page->template->fields as $field){  
Link to comment
Share on other sites

I added a FieldtypePage to the basic-page template to confirm what you're saying and it shows up. And now I can't duplicate the empty Webmentions field returning a null value, so I guess something got fixed when adding a field? No idea. :] I will have to try a fresh install of the module to ensure it works out of the box.

For FieldtypeWebmentions I used the Comments module as a guide since they're very similar. I couldn't find anything specific in the comment module that would determine what's returned for $page->fields. I had tried $page->templates->fields before with the same result, though my understanding is $page->fields is just a shortcut reference to it.

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

×
×
  • Create New...