Jump to content

Method like WireArray::explode() for page


Zeka
 Share

Recommended Posts

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

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

image.png.8f6e1619da97d53e48e53e6017d51f9c.png

  • Like 4
Link to comment
Share on other sites

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

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.

  • Like 5
Link to comment
Share on other sites

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:

59d7f76761b0c_ScreenShot2017-10-06at2_36_08PM.thumb.png.eb547a38a9109cd555324f353f91b5ce.png

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