Jump to content


Photo

How to read a InputfieldImage settings from Pageimage Hook?

FieldtypeImage InputfieldImage hook pageimage

  • Please log in to reply
2 replies to this topic

#1 PawelGIX

PawelGIX

    Jr. Member

  • Members
  • PipPip
  • 20 posts
  • 11

  • LocationPoland

Posted 09 March 2012 - 06:42 AM

I am trying to extend InputfieldImage & FieldtypeImage.
I want something like this:

$page->image->action('param');


When I call method action() i want read settings saved with my ExtendedInputImageField field. Then i want perform action based on that settings.
I have first part. I can save EntendedInputImageField settings when I configure new field.
In this way I save the settings:

public function ___getConfigInputfields() {

$inputfields = parent::___getConfigInputfields();
//[...]
$field = $this->modules->get("InputfieldTextarea");
$field->attr('name', 'advSetting');
$field->attr('value', $this->advSetting? (string) $this->advSetting : '');
$field->label = $this->_("Advenced Settings");
$field->description ='';
$fieldset->add($field);
//[...]
$inputfields->add($fieldset);

return $inputfields;
}
<?php

Pageimage::action Hook

class FieldtypeExtendedImage extends FieldtypeImage {
// [...]
public function init() {
$this->addHook('Pageimage::action', $this, 'action');
}
public function action(HookEvent $event) {
// How can I read 'advSetting' settings here?
}
//[...]
}


#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 09 March 2012 - 09:06 AM

This is a good question, and one of the most advanced questions I've ever seen asked here. Here's how to do it. Unfortunately the $field context isn't available to the Pagefile/Pageimage, though the $page context is (accessible as $this->page from a Pagefile/Pageimage object). Given that, we can find the $field context like this:


public function init() {
    $this->addHook('Pageimage::action', $this, 'pageimageAction');
}


public function pageimageAction(HookEvent $event) {

    $field = null; // where we'll keep the field we're looking for
    $image = $event->object;
    $page = $image->page; 
    $action = $event->arguments[0]; 

    // find all fields of type FieldtypeImage that are part of the page we're using
    $imageFields = $page->fields->find('type=FieldtypeImage');

    // loop through to find the one we're looking for
    foreach($imageFields as $imageField) {

        // good to get unformatted in case it's a single image field, 
        // because it'll still be an array rather than 1 image
        $pagefiles = $page->getUnformatted($imageField->name);

        // if the image's pagefiles property matches the one with the 
        // field we're looking at, we have a match. save in $field
        if($image->pagefiles === $pagefiles) {
            $field = $imageField;
            break;
        }
    }

    if($field) {
        $out =  "<ul>" .
                    "<li>Field: {$field->name}</li>" . 
                    "<li>Action: $action</li>" . 
                    "<li>Filename: {$image->filename}</li>" . 
                    "<li>advSetting: {$field->advSetting}</li>" .
                    "</ul>";

        $event->return = $out;
    }
}


To examine the result, do this:

// single image field
echo $page->image->action('test'); 

// multi image field
foreach($page->images as $image) {
    echo $image->action('test'); 
}

I will look closer at adding the $field context to the pageimages/pagefiles/pagefile/pageimage instances in the near future so that it's not necessary to find it yourself like this. But this is the best way to go for now.

#3 PawelGIX

PawelGIX

    Jr. Member

  • Members
  • PipPip
  • 20 posts
  • 11

  • LocationPoland

Posted 09 March 2012 - 01:20 PM

Thanks Ryan.
It works great ;).
It would be nice to be able to get $field contex, in each field. At the time when you need it.





Also tagged with one or more of these keywords: FieldtypeImage, InputfieldImage, hook, pageimage

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users