Blog Archive

Thursday 7 February 2013

The ins and outs of inline editing in FUEL CMS

FUEL CMS automatically allows inline editing of CMS content from the front end of a site. If you are logged in as an administrator,a tool bar will appear on the front end allowing you to edit content such as page titles, meta descriptions and body content there and then, thus making a trip to the admin redundant. Content is made editable via modal dialogue forms. The editable fields are defined in application/config/MY_fuel_layouts.php and the array key defining a layout should match a view file in views/_layouts.

Controllers and CMS content


It is of course possible to create controllers and views that also mix in CMS content - just fetch the CMS fields for a given url and combine the controller content (whatever that might be - from a model, or variables) and make sure the view can show both.

For example a (v1.0) controller like the following:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Rendercms extends CI_Controller {
  
    public function __construct() {
        parent::__construct();
    }
  
    public function index()
    {
        $params;
        $view = 'rendercms/index';
        $vars = array();
        $cms_vars = array();      
        $vars['some_controller_content'] = "Me? I'm controller content!";
        $cms_vars = $this->fuel->pagevars->retrieve($this->uri->uri_string());
        $all_vars = array_merge($cms_vars, $vars);
      
        if($this->input->get('cms') == 'true') {
            $params = array('render_mode' => 'cms');      
        }

        $this->fuel->pages->render($view, $all_vars, $params);  
    }

}

 
combined with a view file (views/rendercms/index.php) such as:

<?php echo fuel_var('body', 'No CMS content here, sorry'); ?>
<hr>
<?php echo $some_controller_content; ?>
<hr>
<?php echo $some_variables_content; ?> 

<hr> 
<p>And I'm humble view HTML</p>
 
can demonstrate the features of FUEL CMS's abilities. The view file will show any CMS content at the url /rendercms (or simply default to 'No CMS content here...'), it can also show the 'some_controller_content' created by the controller, and, as a bonus, it picks up a variable defined in views/_variables/rendercms.php:

<?php

$vars['some_variables_content'] = "I'm only variable content";

?>


So far so good! However, notice the FUEL toolbar on this page (you need to be logged in to the admin, remember, to see this). It's there, but there is nothing it can do to edit the CMS content! Those features, the "toggle editable areas" and "toggle page's publish status" and the rest, are unavailable.

Depending on your needs, this may be only a mild inconvenience. But is there a way round it - a way to get everything!?

The answer is yes, but with some drawbacks.

Everything


In the controller above, you will notice I added a conditional to check for a parameter "cms=true".

Add that parameter to the url (/rendercms?cms=true) and refresh the page (NB make sure in config.php you have allowed GET: $config['allow_get_array']  = TRUE;).

Now only the CMS content is visible, in whatever layout file the CMS has defined (probably 'main' if nothing else). The variables and controller content and even the view file itself are now ignored because the render_mode => 'cms' specification (the default value is 'views'). The 'main' layout file alone is being used, and that, of course, lacks your custom controller content!

So, if there was a layout file that could echo the controller, variable AND CMS content, we'd win!

Make a file ("rendercms.php") in the views/_layouts folder that includes all these variable placeholders eg:

<html>
<head><title><?php echo fuel_var('page_title'); ?></title></head>
<body style="font-family: Georgia;">
<h1>This is a layout file</h1>
<?php echo fuel_var('body', 'No CMS content here, sorry'); ?>
<hr>
<?php echo $some_controller_content; ?>
<hr>
<?php echo $some_variables_content; ?>
</body>
</html>


Now go back to your CMS page in the admin, for "rendercms". In the layout field (you may need to refresh the admin page to reload the options) you should see "rendercms" as a layout. Select it, and save the page. Now when you return and refresh /rendercms?cms=true you should see CMS fields that are editable from the toolbar, controller content and variables content combined.

The additional drawback to this solution, however, is that to make the 'layout variables' fields appear in the admin, you will also need to add an array for "rendercms" in MY_fuel_layouts.php. eg:

$config['layouts']['rendercms'] = $config['layouts']['main'];

Here for example, we copy all the fields for the 'main' layout to the new 'rendercms' layout.

So to make a consistent inline editing experience for the user, where you have CMS & controller content combined, you need to

  1. make a layout file and 
  2. a layout configuration array to support it, in addition to 
  3. the controller, 
  4. view 
  5. (and optionally) the variables file.

It's conceivable though, that you could make just one layout file, and make it contain all the possible controller content it might need to accommodate, eg by adjusting its own layout according to which (controller) url it is responding too. Conceivable, but possibly monstrous! The viability of that solution would depend on the scale, complexity and number of your controllers.

If however inline editing isn't such a priority, the original method outlined above is pretty simple to effect. If you want, that functionality can be moved to MY_Controller, and controllers that combine CMS and their own content can be extended from that.

No comments:

Post a Comment

My top artists