0

I'm creating a new block and I want to pass a defined variable to the block instance on add.

In my controller, I have the following:

// declare the var
public $hasMap = 0;

public function add() {
    $this->set('hasMap', $this->generateMapNumber());
}

The generateMapNumber() function looks like this:

public function generateMapNumber() {
    return intval(mt_rand(1,time()));
}

In my add.php form I have a hidden field:

<?php $myObj = $controller; ?>
<input type="hidden" name="hasMap" value="<?php echo $myObj->hasMap?>" />

When I create a new block, hasMap is always 0 and the hidden input value is always 0 too. Any suggestions? Thank you!

--- EDIT ---

From the concrete5 documentation:

// This...
$controller->set($key, $value)
// ... takes a string $key and a mixed $value, and makes a variable of that name 
// available from within a block's view, add or edit template. This is 
// typically used within the add(), edit() or view() function
Jongosi
  • 2,305
  • 1
  • 28
  • 31
  • I don't know anything about concrete5, but is `add()` even called before? And does `set()` work as intended (so you can call it as object property)? – dan-lee Sep 12 '12 at 06:41
  • Yes, `add()` is called at a higher level anyway, and my `add()` is intercepted in the controller. `set()` is also the correct way to pass data to the form, according to the C5 docs. I've looked at other working examples within C5 that they've written and it all works, which makes this all the more confusing :( – Jongosi Sep 12 '12 at 08:26
  • If I set `public $hasMap = 12;`, for example, I get 12 in the hidden input's value, but it just ignores the `add()` function. See http://www.concrete5.org/documentation/developers/blocks/mvc-approach – Jongosi Sep 12 '12 at 08:35
  • 1
    And if you call `generateMapNumber` on `$myObj` explicitely in your `add.php`? – dan-lee Sep 12 '12 at 09:29
  • That's a great suggestion Dan - I'll try it when I get to my machine, thx. – Jongosi Sep 12 '12 at 09:51

3 Answers3

4

Calling $this->set('name', $value) in a block controller sets a variable of that name with the given value in the appropriate add/edit/view file -- you don't need to get it from within the controller object. So just call <?php echo $hasMap; ?> in your add.php file, instead of $myObj->hasMap.

Jordan Lev
  • 2,773
  • 2
  • 26
  • 39
0

It will not be the same value, because the function will give diferrent values every timy it is called.

0

So here's the solution. In the controller...

public $hasMap = 0;

// no need for this:
// public function add() {  }

public function generateMapNumber() {
    if (intval($this->hasMap)>0) {
        return $this->hasMap;
    } else {
        return intval(mt_rand(1,time()));
    }
}

And then in the add.php file...

<?php $myObj = $controller; ?>

<input type="hidden" name="hasMap" value="<?php echo $myObj->generateMapNumber()?>" />

It works perfectly. On add, a new number is generated and on edit, the existing number is drawn from the hasMap field in the db.

Thanks for all the input. Hope that helps someone else!

Jongosi
  • 2,305
  • 1
  • 28
  • 31
  • 1
    This is a terribly complicated way to achieve what you want. There is absolutely no reason to go through all that trouble -- just call `$this->set('hasMap', $this->generateMapNumber())` in the `add()` function, then access `$hasMap` in the add.php file (as explained in my answer). – Jordan Lev Sep 12 '12 at 21:08
  • Thx Jordan, if you read the original question you would see that I was doing exactly that. However, `$hasMap` was always 0, even on edit. In the question I put the value in the field as `hasMap?>` but believe me I tried `$hasMap` too. – Jongosi Sep 13 '12 at 08:08
  • 1
    No, if you read the original question, you see that you are *not* doing exactly that -- instead you are doing `$myObj->hasMap`. You now say that you tried just "$hasMap", but it is not in the question you asked, so how would anyone know that? – Jordan Lev Sep 13 '12 at 15:18