Adding Custom Fields to Pages

edited March 2018 in Modules
I previously hadn't used FuelCMS Pages and instead had static views with custom modules. I now have a project where each page needs to have editable content, so I've been reading the docs and forums with some success.

I created application/views/_layouts/home.php, which is the same as main.php.
<?php $this->load->view('_blocks/header')?> <?php echo fuel_var('body', ''); ?> <?php $this->load->view('_blocks/footer')?>

In application/views/home.php, I added:
<?php fuel_set_var('page_title', 'Title'); fuel_set_var('meta_description', 'This is a description.'); fuel_set_var('layout', 'home'); ?>

And then in application/config/MY_fuel_layouts.php, I added:
$config['layouts']['home'] = array( 'fields' => array( 'Banner' => array('type' => 'fieldset', 'label' => 'Body', 'class' => 'tab'), 'heading' => array('label' => lang('layout_field_heading')), 'tagline' => array('label' => lang('layout_field_tagline')), 'body' => array('label' => lang('layout_field_body'), 'type' => 'textarea', 'description' => lang('layout_field_body_description')), ) );

And then back in application/views/home.php to use the custom fields:
<div class="heading"> <h1><?php echo fuel_var('heading'); ?></h1> <h2><?php echo fuel_var('tagline'); ?></h2> </div>

In the CMS, home is an available layout, the custom fields are there, and the content entered displays on the Home page. However, I have this error:
A PHP Error was encountered Severity: Notice Message: Undefined variable: wildcard_location Filename: models/Fuel_pages_model.php Line Number: 233 Backtrace: File: /home/public_html/fuel/modules/fuel/models/Fuel_pages_model.php Line: 233 Function: _error_handler File: /home/public_html/fuel/modules/fuel/libraries/Fuel_pages.php Line: 650 Function: find_by_location File: /home/public_html/fuel/modules/fuel/libraries/Fuel_pages.php Line: 558 Function: assign_location File: /home/public_html/fuel/modules/fuel/libraries/Fuel_pages.php Line: 525 Function: initialize File: /home/public_html/fuel/modules/fuel/libraries/Fuel_pages.php Line: 65 Function: __construct File: /home/public_html/fuel/modules/fuel/controllers/Page_router.php Line: 70 Function: create File: /home/public_html/index.php Line: 364 Function: require_once

What's the problem?


  • edited 1:01PM
    I also tried creating application/views/_variables/home.php and removing fuel_set_var('layout', 'home'); from application/views/home.php, but it didn't help. If I delete the page from the CMS, the error goes away. I know it's just a notice, but I'd rather not have an undefined variable if possible.
  • edited 1:01PM
    There appears to be an issue in the model. I've posted the following fix to the dev branch:
  • edited 1:01PM
    Hello again. I tried that (though it was missing a closing parenthesis), but then the Home page doesn't load at all, just the header and footer blocks.
  • edited 1:01PM
    I also have a question about inline editing. The docs say "A FUEL logo will be displayed in the upper right area of the page that can slide out and provide you the ability to toggle inline editing, publish status and caching. Clicking the inline editing pencil will toggle inline editing on." The only options I have are "Back to Admin" and "Logout".
  • edited 1:01PM
    I realized if I switch it to if (empty($data) OR ($data['location'] != $location AND (!empty($wildcard_location) AND $data['location'] != $wildcard_location.'/:any'))) that the full toolbar displays at the top, but still having an issue with the Home page not loading. Why is it missing my home.php view? Like I mentioned, if I leave Fuel_pages_model.php as it was before, the page loads with an error and no inline editing capability.
  • edited 1:01PM
    I still need assistance with this when you have a moment.
  • edited 1:01PM
    If you are seeing a Back to Admin and Logout button only it's most likely that you are viewing a page that is not editable in the CMS and is instead just a static view file correct? Also, do you have the $config['fuel_mode'] = 'AUTO'; in fuel/application/config/MY_fuel.php?
  • edited March 2018
    It is set to $config['fuel_mode'] = 'auto';
    And it is editable in the CMS, as seen here:
    Looking at the steps in my original post, have I done something wrong or missed a step? The heading and tagline fields display on my Home page using <?php echo fuel_var('heading'); ?>, but I still get the "Undefined variable: wildcard_location" error. If I update Fuel_pages_model to if (empty($data) OR ($data['location'] != $location AND (!empty($wildcard_location) AND $data['location'] != $wildcard_location.'/:any'))) then only my header and footer blocks load.
  • edited 1:01PM
    This project deadline is coming up, your help would be much appreciated!
  • edited 1:01PM
    Try using a lowercase "Home".
  • edited 1:01PM
    Also, try merging in from the develop branch where that wildcard fix has been added:
  • edited 1:01PM
    I'm using lowercase "home" everywhere already. And I've tried with and without the wildcard fix; they both give me different issues. Without the fix, I get the "Undefined variable: wildcard_location" error, but the page loads with the custom fields. If I apply the fix, the home view doesn't load at all (besides header and footer blocks). Does the code from my original post look correct?
  • edited March 2018
    On line 231, below $data = $this->find_one_array($where, 'location desc');
    Add the following to echo out the query to test at line 231 in the Fuel_pages_model file:
    $this->debug_query(); echo '<pre>'; print_r($data); echo '</pre>'; exit();
    This is the query and associated return data that finds the data in the fuel_pages table. What does it print out?
  • If you just want to store the page data in the CMS and incorporate that in your view files, you can call $CI->fuel->pagevars->retrieve('my_location') and it will return an array of just the data associated with the location. You can then use a controller to display the page (controllers take precedence over pages created in the CMS). If you don't want any pages rendered through the CMS, you can change MY_fuel.php $config['fuel_mode'] = 'views';

  • That sounds like what I want to do, but I'm not sure how to achieve it. I already created a home page in the CMS, and created application/views/home.php (which is where the HTML is and where I'd like specific variables that can be edited through the CMS Pages module), application/views/_layouts/home.php (which is currently just a copy of main.php, not sure if correct), and $config['layouts']['home'] in application/config/MY_fuel_layouts.php which has my custom fields for heading and tagline. So now I need to create application/controllers/home.php?

    This is my attempt, still unsure how to pass custom variables:
    <?php class Home extends CI_Controller { function __construct() { parent::__construct(); } function home() { $vars = array(); $vars = array('heading' => 'I want this editable in the CMS', 'tagline' => 'Same with this'); $page_init = array('location' => 'home'); $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init); $this->fuel_page->add_variables($vars); $this->fuel_page->render('home', $vars); } } ?>

    Where does $CI->fuel->pagevars->retrieve('home') need to go? Do I need to create application/views/_variables/home.php as well? Can I delete any of the files/functions mentioned in my original post? Thank you for your time.

  • I think you want $this->fuel->pages->render(....):

  • I have $this->fuel_page->render('home', $vars); Should it be something else?

  • Note that it's $this->fuel->pages->render()... That is an older syntax (but may still work). Regarding where to put $CI->fuel->pagevars->retrieve('home'), I would merge it with your $vars variable since that's all it is is an array of variables to pass to your page.

  • I now have:
    <?php class Home extends CI_Controller { function __construct() { parent::__construct(); } function home() { $vars = array(); $vars = $CI->fuel->pagevars->retrieve('home'); $this->fuel->pages->render('home', $vars); } } ?>
    Does that look correct? I still have <?php echo fuel_var('heading'); ?> in my home view for example, but the text from the CMS isn't loading.

  • If you print_r on $vars, is the variable in their?

  • When I add <?php print_r($vars); ?> to application/views/home.php, this happens:
    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 57525451 bytes) in /home/public_html/fuel/modules/fuel/libraries/Fuel_pages.php on line 1355<br /> A PHP Error was encountered<br /> Severity: Error<br /> Message: Allowed memory size of 134217728 bytes exhausted (tried to allocate 57525451 bytes)<br /> Filename: libraries/Fuel_pages.php<br /> Line Number: 1355<br /> Backtrace:

  • OK. That's because their is probably an infinite loop to referencing the $CI object and print_r can't handle it. Try print_r(array_keys($vars)) and see if the the name of the variable is in that array.

  • I just got back from vacation, ready to try and solve this again! Haha.

    Array ( [0] => layout [1] => page_title [2] => meta_keywords [3] => meta_description [4] => js [5] => css [6] => body_class [7] => CI )

    The array does not contain heading or tagline.

    Whenever I click on home in the Pages module, I get the message, "There is an updated view file located at /public_html/fuel/application/views/home.php. Would you like to upload it into the body of your page (if available)?" I've been clicking No, because there is no body field. Should I hit yes?

    The location is set to home, layout is set to home, and layout variables for Heading and Tagline are filled in. What else can I try?

  • Hit no for the import.

    If you go to the "home" page under Pages in the CMS, does it having your heading and tagline fields?

  • Yes, it does.

  • So you see them saved in the database under fuel_page_variables with the home's page_id in the page_id column?

  • Both entries are there with page_id = 1 (which is home).

  • Has the fuel_mode in fuel/application/config/MY_fuel.php been set to auto (e.g. $config['fuel_mode'] = 'auto';)

    Also, in your controller you have:


    Try replacing $CI with $this

  • When I set fuel_mode to auto (instead of views), only the header and footer blocks load. Updated the controller to use $this instead of $CI, but it didn't make a difference. Array is still the same as it was before: Array ( [0] => layout [1] => page_title [2] => meta_keywords [3] => meta_description [4] => js [5] => css [6] => body_class [7] => CI ).

  • You'll definitely want it set to "auto" instead of "views" if you are wanting to pull data from the CMS. If you create another page in the CMS does it pull in more data than home?

Sign In or Register to comment.