Jump to content

Recommended Posts

Posted

Hi,

I'm trying to build my first module:

It should create a css file for responsive background images. In order to do this I'm searching for a specific field (section_img)  and try to write all thumbs - build with Apeisa Thumbnails Module - to an array. After this I'm looping through this array and return the css structure.

I first tried out this code on a normal page-template and everything works fine. Now when I try to execute the code I get an error:

Session: Method Pageimages::getThumb does not exist or is not callable in this context

Here's an excerpt of the code:

public function createcss($event) {

$slides = $this->pages->find("section_img!=''");

$name = array();
$smp = array();
$sml = array();
$lmp = array();
$lml = array();
$tap = array();
$def = array();

foreach($slides as $s) { 
	$bgImg = $s->section_img;
	array_push($name,$s->name);
	array_push($smp,$bgImg->getThumb('small-mobile-portrait'));
	array_push($sml,$bgImg->getThumb('small-mobile-landscape'));
	array_push($lmp,$bgImg->getThumb('large-mobile-portrait'));
	array_push($lml,$bgImg->getThumb('large-mobile-landscape'));
	array_push($tap,$bgImg->getThumb('tablet-portrait')); 
	array_push($def,$bgImg->url); 
};
// Small Mobile - Portrait
$out = "@media only screen and (max-width: 320px) {.slide.imgContainer{min-height:20rem;}";
    
    for($i = 0; $i <= 5; $i++) {
        $out.=".section_".$name[$i]."{background-image:url('".$smp[$i]."');}";
     }
$out.= "}";
// Small Mobile - Landscape
$out.= "
@media only screen and (min-width: 320.1px) and (max-width: 480px) {.slide.imgContainer{min-height:30rem;}";
    for($i = 0; $i <= 5; $i++) {
        $out.=".section_".$name[$i]."{background-image:url('".$sml[$i]."');}";
     };
$out.= "}";
// Large Mobile - Portrait
$out.= "
@media only screen and (min-width: 320.1px) and (max-width: 480px) and (orientation: portrait) {.slide.imgContainer{min-height:30rem;}";
    for($i = 0; $i <= 5; $i++) {
        $out.=".section_".$name[$i]."{background-image:url('".$lmp[$i]."');}";
     };
$out.= "}";
// Large Mobile - Landscape
$out.= "
@media only screen and (min-width: 480.1px) and (max-width: 960px) and (orientation: landscape) {.slide.imgContainer{min-height:30rem;}";
    for($i = 0; $i <= 5; $i++) {
        $out.=".section_".$name[$i]."{background-image:url('".$lml[$i]."');}";
     };
$out.= "}";
// Tablets - Portrait
$out.= "
@media only screen and (min-width: 480.1px) and (orientation: portrait) {.slide.imgContainer{min-height:50rem;}";
    for($i = 0; $i <= 5; $i++) {
        $out.=".section_".$name[$i]."{background-image:url('".$tap[$i]."');}";
     };
$out.= "}";
// Default Desktop
$out.= "
@media only screen and (min-width: 960.1px) {";
    for($i = 0; $i <= 5; $i++) {
        $out.=".section_".$name[$i]."{background-image:url('".$def[$i]."');}";
     };
$out.= "}";

$css = fopen("/css/resbg.css", 'w');
fwrite($css, $out);
fclose($css);

}

Any ideas how I con use the ->getThumb() method working?

Many greets, Jens.

Posted (edited)

You should check what you get with your find.

For debugging you can drop in something like

var_dump($slides);
die('Manually debugged ' .  __FILE__ . ':' . __LINE__);

or inspect the $s and not the $slides, or both.

EDIT: should have read more carefully before try to answer. Soma gave the right direction!

Edited by horst
Posted

"Pageimages::getThumb"

PageImages is an array, but you need to get one image object

$image->first()->getThumb()

Or just change you image field to 1 max image

------

Also 

$this->pages->find("section_img!=''")

maybe rather: 

$this->pages->find("section_img.count>0")

since image field is an array, it's not a text

  • Like 3
Posted

Or just change you image field to 1 max image

I've done this before.

EDIT: Sorry, didn't know that this will have no effect if trying to target the field form a module

I've change this line:

$slides = $this->pages->find("section_img!=''");

to

$slides = $this->pages->find("section_img.count>0");

but still get this error:

Session: Method Pageimages::getThumb does not exist or is not callable in this context.

@Horst and Soma: This code I've posted first was working when it was a template file. Now I'm trying to migrate it to a module in order not to be loaded everytime a page is visited.

This is how I init the funktion:

public function init() {

$this->pages->addHookAfter('save', $this, 'createcss');

}

Any ideas?

Posted

Ah youre in module so no ouputformat so an file.field is always a wirearray. Hence the Pageimages error and not Pageimage.

Posted

Ah youre in module so no ouputformat so an file.field is always a wirearray. Hence the Pageimages error and not Pageimage.

Can you please explain what you mean exactly?

Posted

It what I wrote at first its an array not a single image so you need to get it with first(). Sorry am on mobile here. Maybe someone else care to explain.

Posted

You are my heros !!! Thank you so much!!!

What Soma is getting at is that because you are in a module context, the "image field to 1 max image" setting doesn't work to convert the image field from an array to a single image, so you must use first().

Is this somewhere documented?

Posted

Glad you got it working.

Just to clear things up for people searching the forums, I just edited the title of this post from the original: "How To Use A Method From Another Module In Own Module" since this is more specifically about image fields.

I don't think it really is documented anywhere at the moment - definitely something that should be explained somewhere.

  • Like 1
Posted

Glad you got it working.

Just to clear things up for people searching the forums, I just edited the title of this post from the original: "How To Use A Method From Another Module In Own Module" since this is more specifically about image fields.

I don't think it really is documented anywhere at the moment - definitely something that should be explained somewhere.

Thx again Adrian. btw: is it possible to edit the title by myself?

Posted

Thx again Adrian. btw: is it possible to edit the title by myself?

Yes. You would need to edit your post using the 'full editor'.....

  • Like 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...