Book Image

concrete5 Cookbook

Book Image

concrete5 Cookbook

Overview of this book

concrete5 is an increasingly popular open source content management system because of its incredible and easy-to-use interface. But, it also comes complete with a robust PHP framework, allowing web developers to create custom websites and applications with ease. "concrete5 Cookbook" is a practical collection of solutions to tasks that both novice and experienced concrete5 developers face on a regular basis. Readers will learn multiple subjects, including full blueprints for developing an event calendar add-on and an image gallery block. Developers new to concrete5 will quickly learn how to customize concrete5 to their needs, and seasoned pros will find it an excellent quick reference for performing specific tasks. "concrete5 Cookbook" will transform ordinary PHP developers into concrete5 experts capable of bending concrete5 to their will and unleashing the true power of this up-and-coming content management system. Throughout the course of over 140 recipes and 3 bonus project blueprint chapters, PHP developers will learn how to create custom blocks and dashboard interfaces as well as programmatically work with pages, files, users, permissions, and more. Discover the built-in Active Record support that makes working with databases simple and maintainable. Readers will also learn how to take advantage of the numerous helper classes included in concrete5, and will dive deep into the concrete5 MVC framework to create powerful custom websites and applications. Tie together all of the concepts learned in the recipes with 3 bonus chapters featuring complete blueprints to create a calendar add-on, an image gallery block type, and tips on how to sell your themes and add-ons for money! "concrete5 Cookbook" is a complete collection of recipes to solve the most common (and some not-so-common) tasks that concrete5 developers will face on a regular basis.
Table of Contents (19 chapters)
concrete5 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the custom block type


If you recall, we created some boilerplate files for our block type, but it doesn't actually do anything. We will want to create a block that shows a list of the events entered into the database.

First, let's add the HTML form to form.php that will be displayed whenever site editors add or edit the block on the site:

<?php
defined('C5_EXECUTE') or die(_("Access Denied.")); 
$form = Loader::helper('form');
?>
<div class="ccm-ui">
   <table class="table table-striped table-bordered">
      <tr>
         <td>
            <?php echo t('Maximum events to show') ?>
         </td>
         <td>
            <?php echo $form->text('item_limit', $item_limit) ?>
         </td>
      </tr>
   </table>
</div>

Great! Let's include this file in both add.php and edit.php. Add the following to each of those files:

<?php 
defined('C5_EXECUTE') or die(_("Access Denied.")); 
include('form.php');

Alright! All we have to do now is create the frontend view of the block. Open view.php in your code editor and enter the following HTML and PHP code:

<?php defined('C5_EXECUTE') or die(_("Access Denied.")); ?>
<h1 class="events-title"><?php echo t('Events Calendar') ?></h1>
<div class="events-list">
   <?php if (!empty($events)): ?>
      <?php foreach ($events as $event): ?>
         <div class="event-item">
            <h2><?php echo $event->title ?></h2>
            <div class="event-date">
               <?php echo $event->getDate() ?>
               <?php if ($event->location): ?>
                  @ <?php echo $event->location ?>
               <?php endif; ?>
            </div>
            <?php if ($event->description): ?>
               <div class="event-description">
                  <?php echo $event->description ?>
               </div>
            <?php endif; ?>
         </div>
      <?php endforeach; ?>
   <?php else: ?>
      <p><?php echo t('There are no events!') ?></p>
   <?php endif; ?>
</div>

That looks great! Here we are essentially looping through the $events array and displaying a row for each event. We again make use of the getDate() function on the event model to output a clean and friendly date.

Finally, let's add some simple CSS styles to view.css:

h1.events-title {
    margin-bottom: 30px !important;
}
.event-item {
    padding: 20px 0;
    border-bottom: 1px solid #ddd;
}
.event-item:last-child {
    border-bottom: 0;
}
.event-item p {
    margin: 0 !important;
}
.event-date {
    font-size: 12px;
    color: #888;
}

Nice! Now we are ready to see it in action!