How to $bvt->setBlockCustomTemplate ?

Permalink 2 users found helpful
Hey guys,

I've been trying to use the $bvt->setBlockCustomTemplate in a block I'm creating but I have a hard time applying the set with the data I specify in the Block Edit mode when saved.

What I did in my EDIT form is:
<?php 
    global $c;
   $bt = $controller->getBlockIdCarl();
   $b = $controller->getBlockTypeIdCarl();
   $templates = $bt->getBlockTypeCustomTemplates();
   $txt = Loader::helper('text');
    ?>
       <select name="settemplate" id="settemplate">
         <option value="">(<?php echo t('None selected')?>)</option>
         <?php  foreach($templates as $tpl) { ?>
            <option  value="<?php echo $tpl?>" <?php  if ($b->getBlockFilename() == $tpl) { ?> selected <?php  } ?>><?php    
               if (strpos($tpl, '.') !== false) {
                  print substr($txt->unhandle($tpl), 0, strrpos($tpl, '.'));
               } else {
                  print $txt->unhandle($tpl);



and in my controller SAVE function I've added this:
public function save($data) { 
               $b = $this->getBlockObject();
          $bvt = new BlockViewTemplate($b);
          $bvt->setBlockCustomTemplate($data['settemplate']);


So I have all the templates available for that block generated in my SELECTBOX no problems but when I select one and hit SAVE, it's does not apply it and it does not return me any error.

Help ? lol

Thanks

olacom
 
Mnkras replied on at Permalink Reply
Mnkras
One of Remo's Free blocks does that, look around in the MP
olacom replied on at Permalink Reply
olacom
Hi Mnkras, are you sure it was remos ? Because I checked all of his addon and none of them have this in it.
Mnkras replied on at Permalink Best Answer Reply
Mnkras
yep, his expand collapse, your trying to get custom templates in the edit dialog right?

//in edit form
  <select name="layout">
    <option value="">(<?php   echo t('None selected') ?>)</option>
    <?php   
    $templates = $controllerObj->getTemplates();
    $currentTemplate = $controllerObj->getCurrentTemplate();
    foreach ($templates as $template) {
      $selectedTemplate = "";
      if ($currentTemplate == $template) {
         $selectedTemplate = ' selected="selected" ';
      }
      echo "<option {$selectedTemplate} value=\"{$template}\">{$template}</option>";
    }
    ?>
  </select>


//controller
    function getTemplateByDirectory()
    {
      $templates = scandir(dirname(__FILE__) . '/templates');
      $templates = array_filter($templates,array($this,'filterDirectories'));
      return $templates;
    }
    /**
     * Returns all available templates for this block
     */         
    function getTemplates()
    {
      $blockObject = $this->getBlockObject();
         if (is_object($blockObject)) {
            $bt = BlockType::getByID($blockObject->getBlockTypeID());


and in the save function you need
$blockObject = $this->getBlockObject();
       if (is_object($blockObject)) {
          $blockObject->setCustomTemplate($data['layout']);
       }


I think thats all the code you need,
Mnkras replied on at Permalink Reply
Mnkras
btw, some of the things he does in here can be swapped out for c5 methods and such
olacom replied on at Permalink Reply
olacom
its working, I was trying to use: setBlockCustomTemplate() instead of setCustomTemplate();

Many thanks. :D
Mnkras replied on at Permalink Reply
Mnkras
NP man :)