C_invoicedetail - Material Description At times we may need to display additional fields from a related entity, for instance in Invoice example we would like to display "Material Description" along with "Material Number". When adding a new derived field there are two components to address
view (using ListViewColumnAdapter) and
designer (using an element).
Lets add Material Description field in "C_invoicedetailsRelatedListView"
array('cells' =>
array(
array(
'elements' => array(
array('attributeName' => 'null', 'type' => 'MaterialDescription'),
),
),
)
),
Now we need to create a MaterialDescriptionListViewColumnAdapter
class MaterialDescriptionListViewColumnAdapter extends TextListViewColumnAdapter
{
public function renderGridViewData()
{
return array(
//Since in the view we are passing "null" attribute we need to specify the attribute name here
//in the view 'attributeName' => 'null' must be null
'name' => 'c_material', // $this->attribute,
'header' => Yii::t('Default', 'Material Description'),
//$data passed to view has field c_material of type C_material which as attribute description
'value' => '$data->c_material->description',
'type' => 'raw',
);
}
}
You check out other ListViewColumnAdapter for example as well.
Element At this point our view will contain the material description but C_invoicedetailsRelatedListView layout in designer will be broken
(Admin -> Designer Home » Inv. Details » Layouts » Related List View - Inv. Details For Invoice).
To fix this we need to add "derivedAttributeTypes" in our view and MaterialDescriptionElement.
derivedAttributeTypes in View
'derivedAttributeTypes' => array(
'MaterialDescription',
),
MaterialDescriptionElement
//
//Please note I am not 100% on the element code but it's working for me
//
class MaterialDescriptionElement extends Element implements DerivedElementInterface
{
protected function renderEditable()
{
throw NotSupportedException();
}
protected function renderControlEditable()
{
throw NotSupportedException();
}
/**
* Render the full name as a non-editable display
* @return The element's content.
*/
protected function renderControlNonEditable()
{
assert('$this->attribute == "null"');
assert('$this->model->{$this->attribute} instanceof C_material');
$materialModel = $this->model->{$this->attribute};
return Yii::app()->format->text($materialModel->description);
}
protected function renderLabel()
{
return Yii::t('Default', 'Material Description');
}
public static function getDisplayName()
{
return Yii::t('Default', 'Material Description');
}
/**
* Get the attributeNames of attributes used in
* the derived element.
* @return array of model attributeNames used.
*/
public static function getModelAttributeNames()
{
return array();
}
public static function isReadOnly()
{
return true;
}
}
End Result