Jump to content

How to read a InputfieldImage settings from Pageimage Hook?


PawelGIX
 Share

Recommended Posts

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?
     }
     //[...]
}
  • Like 1
Link to comment
Share on other sites

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.

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