Advanced Content Manager pour Magento 1

PHP helper methods, render methods

Renders methods

The following methods are made to help you render your content. They are all based on a content model object: Banana_ContentManager_Model_Content.

To get a content, you can load it by his ID, or by collection of several contents (see getCollection() section of this page.

To get a content object by ID:

$content = Mage::getModel('contentmanager/content')->load(123456);

Get all raw values

$content->getData();

Get a specific value by his field identifier.

  $content->getData('field_identifier');
  $content->getAttributeText('field_identifier'); // get the option label for a select field type.
  $content->getFieldIdentifier();

Get content type model

$content->getContentType();

Get image URL for an image field type.

$content->getImage($attributeName, $width = NULL, $height = NULL, $original = false);

$width: width in pixels, if we want a resized image, set to NULL for proportionnal resize with a setted $height
$height: height in pixels, if we want a resized image, set to NULL for proportionnal resize height a setted $width
$original: force to return the orignal instead of cropped image, false by default

Get file absolute URL, for a file field type

$content->getFile($field_identifier);

Get content url

$content->getUrl();

For a collection, url_key attribute must be selected.

Get content collection from content relation field

$content->getContentsCollection($attributeName, $attributes);

$attributeName: the string identifier of your field.
$attributes: PHP array, list of the fields you want to select for further use : array('title', 'url_key', 'news_date')

Render a field in HTML

$content->render($element, $params);

$element: the string identifier of your field.
$params: PHP array, extra parameters for your field, will override default ones. Depending on your field type:

  • label: string - The label of the element
  • label_type: int - 1 for showing the label over the field value
    2 for label below the field value
    3 for an inline label
    0 to hide the label
  • html_label_tag: string - The html label tag.
  • html_tag: string - The html element for the whole wrapper of the field value
  • html_id: string - wrapper html id
  • html_class: string - wrapper html css classes
  • has_link: string -
    0 for no link
    1 to wrap the field value with a link to the corresponding content.
  • type: string - The output format of your field, accepted values, depending of your field type are:
    Checkbox: checkbox_list_values, checkbox_list_titles, checkbox_comma_values, checkbox_comma_titles
    Date: date_short, date_medium, date_long
    Date time: date_time_short, date_time_medium, date_time_long
    Drop down: drop_select_title, drop_select_value
    File: file_filename_without, file_filename_with, file_custom_label
    Image: image_cropped, image_original
    Multiple select: multiple_list_values, multiple_list_titles, multiple_comma_values, multiple_comma_titles
    Product: product_all, product_name, product_name_image, product_name_image_price, product_name_price
    Radio: radio_select_title, radio_select_value
  • width: int - Image width
  • height: int - Image height
  • link: int (0 / 1)- Wrap the image by a link pointing to the original image.

Examples

  //render a field, without any extra params
  $content->render('my_description');
  //render a field of type text field, with label over the element
  $content->render('subtitle', array(
   'label' => 'My Label',
   'label_type' => 1,
   'label_html_tag' => 'h3',
   'html_tag' => 'fieldset',
   'html_id' => 'myId',
   'html_class' => 'class1 calss2',
   'has_link' => 1,
  ));
  //render an image field with a link to the original image
  $content->render('subtitle', array(
   'label' => 'My Label',
   'label_type' => 2,
   'height' => 250,
   'width' => 300,
   'type' => 'image_cropped',
   'link' => 1
  ));
  //render a product relation field
  $content->render('products', array(
   'label' => 'Related products',
   'label_type' => 1,
   'type' => 'product_name_image'
  ));

How to get a collection of several contents

You can easily retrieve a contents collection with getCollection() method of Magento.
Apply to it the common methods addAttributeToSelect() and addAttributeToFilter().

Example

  //get all contents of the type news, where title contains Banana
  $contentCollection = Mage::getModel('contentmanager/content')
   ->getCollection('news')
   ->addAttributeToSelect(array('title', 'url_key', 'my_image', 'news_description'))
   ->addAttributeToFilter('title' => array('like' => '%Banana%'));
  foreach($contentCollection as $content)
  {
   echo $content->render('title', array('html_tag' => 'h2'));
   echo $content->render('news_description', array('html_tag' => 'div', 'html_class' => 'std'));
   echo $content->render('my_image', array('width' => 400));
   echo '$content->getUrl().'">'.$this->__('Read more').'';
  }

Display all options for a specific field

This is an example to get all options for a field of type drop down, multiple, radio, or checkboxes, with an identifier of "news_category"

  $option = Mage::getModel('contentmanager/contenttype_option')->load('news_category', 'identifier');
   
  if($option && in_array($option->getType(), array('drop_down', 'multiple', 'radio', 'checkbox')))
  {
      $collectionOptionsValues = Mage::getModel('contentmanager/contenttype_option_value')
          ->getCollection()
          ->addTitleToResult(Mage::app()->getStore()->getId())
          ->addFieldToFilter('option_id', $option->getId());
          $collectionOptionsValues->getSelect()->order('sort_order');
       
      foreach($collectionOptionsValues as $value)
      {
         echo $value->value;
         echo $value->title;
       } 
  }

How to get a menu

Create the block in your layout XML if you are used to, of display it in your phtml files thanks to this following method.
For more information, read our dedicated documentation to the menus.

echo $this->getLayout()->createBlock('contentmanager/menu', 'main')->toHtml();

Dynamic layout handle

A layout handle is a XML tag which allow to determine which blocks are loaded depending on the current path.
For example, contentmanager_index_view is the handle which will be used on a content view page.

The default layout for a content view is:

  <contentmanager_index_view>
      <reference name="head">
          <action method="addCss"><stylesheet>css/banana/content.cssstylesheet>action>
      reference>
      <reference name="content">
          <block type="contentmanager/view" name="contentmanager.content" as="contentmanager_content" />
      reference>
  contentmanager_index_view>

The dynamic layout handle will permits to add specific block or layout for a defined content type.
Let's say we want to add a cms block in the left column of our blog content type. We will use then CONTENT_TYPE_VIEW_blog as specific handle

  <CONTENT_TYPE_VIEW_blog>
      <reference name="left">
          <block type="cms/block" name="my-custom-block">
              <action method="setBlockId"><block_id>my-custom-blockblock_id>action>
          block>
      reference>
  CONTENT_TYPE_VIEW_blog>