Jump to content

Fieldtype find method


gRegor
 Share

Recommended Posts

Solved below

I'm wondering how to properly implement a find() method for a Fieldtype so it can be used in an API-y way. When I set up the FieldtypeWebmentions I used FieldtypeComment as a basis. It includes a find() method, which I'm not entirely sure I understand, or is even what I need.

What I want to do (from elsewhere in my code) is perform a search across the webmention field for a certain author email, like:

$page->Webmention->find('author_email=user@example.com');

I've looked at http://processwire.com/api/modules/ and the Fieldtype Map Marker as an example, but it is still a bit opaque to me what the Right Way™ is. Is it actually the getMatchQuery() method I need to implement? Thanks for any help!

Link to comment
Share on other sites

As far as I understand it you want to put the logic for this in WebmentionArray.php as you want to search the array holding your data.

Maybe just checking out your WebmentionArray can help, too..

Cause it's quiet late I just throw in a link to KeyValueArray.php of FieldtypeKeyValueMultiple and hope it gives you another useful reference...

Most of the stuff comes from FieldtypeEvents, but I needed to add a remove method because the one from parent didn't worked, for whatever reason..

And the get method is custom, I wanted to simplify way user can search for data..

https://github.com/CanRau/FieldtypeKeyValueMultiple/blob/master/KeyValueArray.php

Saludos

Link to comment
Share on other sites

Thanks, that is useful. I had not thought about WebmentionArray. I did some more testing last night and $page->Webmention->find('author_email=user@example.com') did actually work, for searching within the field on a specific page. I also got $pages->find('Webmentions.author_email=user@example.com') to work, regardless of the functions I mentioned. I guess I thought I had to implement some "find" functionality, but it's actually inherited from WireArray.

I should be more clear with my desired use-case: I need a simple boolean check whether someone has sent a webmention previously or not. I was thinking I'd do that by searching, getting a WebmentionArray of results, then using the count() method to see if there's 0 or 1+. I could achieve the same results with the $pages->find selector above, but perhaps there is an even more direct way (short of writing a direct SQL query)?

Link to comment
Share on other sites

maybe a method in WebmentionArray like

public function authorMentioned($email) {
    $query = $this->wire('database')->prepare('SELECT * FROM webmentions WHERE email=?');
    $query->execute(array($email));
    return $query->columnCount();
}

Don't know you db structure, but something like this should work, instead of SELECT * you should provide a single column, e.g. id

And columnCount should be enough cause you just need to know if or not right?

Link to comment
Share on other sites

I haven't done this, but I think you are correct in assuming that getMatchQuery() is what you need to have:

https://github.com/ryancramerdesign/ProcessWire/blob/8eb350b0362c1c70003d3895c6961b3a1655ffc5/wire/core/PageFinder.php#L588

Take a look at kongondo's Matrix for an example of a 3rd party module using this method:

https://github.com/kongondo/FieldtypeMatrix/blob/master/FieldtypeMatrix.module#L507

  • Like 1
Link to comment
Share on other sites

Thanks for those links. I understand better now that getMatchQuery() is used for searching subfields in pages. That function in my fieldtype is adapted from the Comments module and is working as expected. I realized the find() method is for searching the webmention fields outside of a $page context, i.e. in the Webmentions Manager (also adapted from the Comments module).

I ended up adding a find() method to my main Webmention module that finds the first FieldtypeWebmention and calls its find() method directly, so I can call $Webmention->find('author_email=user@example.com') and get the WebmentionArray I want.

  • Like 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

×
×
  • Create New...