Skip to content

Commit

Permalink
CMS Page properties with overriden default implementation returns wrong
Browse files Browse the repository at this point in the history
administration api value. closes #1061
  • Loading branch information
nadar committed Oct 29, 2016
1 parent 88a7d4a commit f981104
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The changelog contains informations about bug fixes, new features or bc breaking
- [#1011](https://github.com/luyadev/luya/issues/1011) The ViewContext implementation for cmslayout rendering allows you now to render other templates inside a cmslayout.
- [#1044](https://github.com/luyadev/luya/issues/1044) Changing the cms permission force menu reload in order to fix bug with old menu permissions.
- [#1013](https://github.com/luyadev/luya/issues/1013) Delete a cms page displays blank page and reloads menu, fixed bug where page was still visible.
- [#1061](https://github.com/luyadev/luya/issues/1061) CMS Page properties with overriden default implementation returns wrong administration api value.

1.0.0-RC1 (04.10.2016)
-----------------------
Expand Down
6 changes: 4 additions & 2 deletions docs/guide1.0/app-cmsproperties.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class MyImage extends \luya\admin\base\ImageProperty
}
```

In order to get use the above MyImage property just run: `<img src="<?= $item->getProperty('myImage')->getValue(); ?>" />`.
In order to get use the above MyImage property just run: `<img src="<?= $item->getProperty('myImage'); ?>" />`.

> As the `$item->getProperty` method returns the Property, as the all propertys implement the `__toString()` they will return the `getValue()` method by default.
## Get the Proprety value

Expand All @@ -95,7 +97,7 @@ A very common scenario is to add properties to an existing menu item like an ima
<a href="<?= $item->link; ?>">
<?php /* now depending on the if the property `navImage` is set for this page item we can access this property object. */
if ($item->getProperty('navImage')): ?>
<img src="<?= $item->getProperty('navImage')->getValue(); ?>" />
<img src="<?= $item->getProperty('navImage'); ?>" /> // equals to: <img src="<?= $item->getProperty('navImage')->getValue(); ?>" />
<? endif; ?>
</a>
</li>
Expand Down
137 changes: 134 additions & 3 deletions modules/admin/src/base/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,167 @@
/**
* Abstract Page Property Class.
*
* @todo remove defaultValue change to initvalue like in blocks!
*
* @author nadar
* Each property must implement this method.
*
* @author Basil Suter <basil.suter@nadar.io>
*/
abstract class Property extends Component implements TypesInterface
{
/**
* @var string The name of the event will be triggered before rendering
*/
const EVENT_BEFORE_RENDER = 'EVENT_BEFORE_RENDER';

/**
* @var string The module where the property is located.
*/
public $moduleName = null;

/**
* @var mixed The value from the database assigned into the property object.
*/
public $value = null;

/**
* @var boolean Whether the property is used for an i18n use case or not, this will
* serialize the input as json into the database and the getValue/getAdminValue methods will
* automatically unserialize the correctly value.
*/
public $i18n = false;

/**
* The internal variable name for this property.
*
* This is like a variable name identifer, this should be unique value across all properties. Allowed
* chars are `a-zA-Z0-9-_`. The defined variable named will be use when retrieving data from a property
* in the frontend. For example `Yii::$app->menu->current->getProperty('varName`)` where varName is the
* varaiable name you choosen as return value of this method.
*
* Example:
*
* ```php
* public function varName()
* {
* return 'myVariable';
* }
* ```
*
* @return string
*/
abstract public function varName();

/**
* The label which is displayed in the administration area.
*
* Example:
*
* ```php
* public function label()
* {
* return 'My Variable';
* }
* ```
*
* @return string
*/
abstract public function label();

/**
* The specifation of what type this property is.
*
* There are different types of variables/propertys to create. Sometimes its
* just a plain text field, textarea or and image or multip image upload. Therefore
* the method `type()` defines what should be created. All types are available als
* constants inside the {{\luya\admin\base\TypesInterface}}.
*
* Example:
*
* ```php
* public function type()
* {
* return self::TYPE_SELECT;
* }
* ```
*
* @see \luya\admin\base\TypesInterface
* @return string
*/
abstract public function type();

/**
* When the object is force to return as string the `getValue()` method is returned.
*
* @return mixed
*/
public function __toString()
{
return $this->getValue();
}

/**
* Options you may have to pass to the selected type.
*
* Sometimes the type of property requires more informations and optional data
* those datas needs to be returned. Example of options to return when using
* the TYPE_SELECT property type:
*
* ```php
* public function options()
* {
* return [
* ['value' => 'ul', 'label' => 'Pointed List'],
* ['value' => 'ol', 'label' => 'Nummeic List'],
* ];
* }
* ```
*
* @return mixed
*/
public function options()
{
return [];
}

/**
* If the property is requested in the frontend or admin context and there is no value
* the `default()` value response will be used.
*
* For example a preselecting item from a list select dropdown:
*
* ```php
* public function defaultValue()
* {
* return 'default';
* }
* ```
*
* @return mixed
*/
public function defaultValue()
{
return false;
}

/**
* The value is passed from the administration area side to the angular view.
*
* @return mixed
*/
public function getAdminValue()
{
if ($this->i18n) {
$this->value = I18n::decode($this->value);
}
return $this->value;
}

/**
* This is what will be returned when the property is requested in the frontend.
*
* You can override this function in order to provide your own output logic.
*
* @return mixed
*/
public function getValue()
{
if ($this->i18n) {
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/src/admin/apis/NavController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function actionGetProperties($navId)
$object = \luya\admin\models\Property::findOne($row['admin_prop_id']);
$blockObject = $object->createObject($row['value']);

$value = $blockObject->getValue();
$value = $blockObject->getAdminValue();

$row['value'] = (is_numeric($value)) ? (int) $value : $value;

Expand Down

0 comments on commit f981104

Please sign in to comment.