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

Adding a page selector field to a form


It is easy to add a page selector field to a form. When clicked on, the sitemap will appear in a modal window, allowing for easy browsing and selecting of a page. This allows for user friendly custom applications that can allow users to attach pages to any type of custom object, like a blog post or cooking recipe, for example.

Getting ready

We will need to use the FormPageSelectorHelper class, which we will need to load using the Loader library.

This recipe will need to be written inside of an HTML form somewhere in your site. We have created a basic single page for this purpose, which can be downloaded with the code from the website for this book. To install the single page, copy the code for this recipe to your concrete5 site directory and visit /dashboard/pages/single. Add a new single page with a path of /example.

How to do it...

concrete5 includes a helper to generate the necessary JavaScript and HTML text needed to show the sitemap selector widget. We will use the Loader class to include and return the PageSelectorHelper class, which will then return the entire HTML that we need. The steps are as follows:

  1. Make the pageSelector helper class available for us to use. The Loader class will automatically instantiate and return the instantiated helper object when the helper gets loaded.

    $pageSelector = Loader::helper('form/page_selector');
  2. Output the HTML and JavaScript required allowing users to select a page.

    echo $pageSelector->selectPage('page_id');

How it works...

The selectPage function returns the HTML and JavaScript necessary to produce a page selector field. There is only one parameter required, and that is a string indicating the name of the field. concrete5 will add a hidden <input> element to the form, which will allow the selected page ID to be read from a POST request.

In this example, once the form is submitted, you could retrieve the selected page ID by looking in the $_POST super variable.

$pageId = $_POST['page_id'];

There's more...

You can specify a page ID in the optional second parameter, which will populate the page selector with an existing page.

$page = Page::getByCollectionPath('/about-us');
echo $pageSelector->selectPage('page_id', $page->getCollectionID());

The pageSelector helper also has a function to display a sitemap in a modal window.

$args = array(
'display_mode' => 'full'
);
$pageSelector->sitemap($args);

Note that the sitemap() function loads an element, which is automatically printed to the screen, so there is no need to echo or print the result.