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 a page type


Creating pages dynamically is a central concept of concrete5 development and can unlock a lot of powerful features in custom applications. Imagine a concrete5 website that has a recipes database. You could make it so, that every time a recipe is added to the database, a new page for that recipe is added to the sitemap, immediately improving the depth of content on your website, its usability, and even search engine performance.

In this example, we will create an "About Us" page and add it to the sitemap.

Getting ready

We are continuing the practice of putting our code in /config/site_post.php for the purposes of demonstration and testing. In real-world use, your dynamic page creation would happen in controller files or in add-on packages.

We will be assigning the new page a page type with the handle of right_sidebar. If this doesn't exist in your own concrete5 installation, please adapt the recipe to suit your needs.

How to do it...

The steps for creating a page are as follows:

  1. Open /config/site_post.php in your code editor.

  2. Load the page type that the new page will use. We will load the page type using its handle (right_sidebar, in this case).

    $pageType = CollectionType::getByHandle('right_sidebar');
  3. Create an associative array that contains the fields that you wish to specify for the new page. In this example, we will only specify the page's name and handle.

    $data = array(
      'cName' => 'About Us',
      'cHandle' => 'about'
    );
  4. Load the parent page that the new page will be placed under. In this case, we are loading the home page, since the new page will be available at the top level.

    $parent = Page::getByID(1);
  5. Add the page by passing the $pageType object and $data array to the add function of the parent Page class.

    $newPage = $parent->add($pageType, $data);
  6. Output a message and exit the process.

    echo 'done!';
    exit;
  7. Visit your site's homepage to execute the code in site_post.php.

  8. If you see the success message, comment out the code in site_post.php so you can visit the newly created page. Otherwise, concrete5 will create a new page every time the site is loaded into a browser.

  9. Visit the newly created page at http://example.com/about.

How it works...

The add() function is a wrapper for the Collection class's add() function. Calling it will create a new Collection record in the database, and a new pending CollectionVersion record (which will be approved if the cvIsApproved variable is set to true or left blank). Then the new page will inherit the necessary permissions and get all of the blocks and other attributes from the Master Collection of the related page type.

There's more...

You will need to provide two parameters to the add() function, a CollectionType object (which we worked with earlier in this chapter), and an associative array containing all of the data needed to create the page. The associative array can contain the following fields.

Attribute

Required?

Description

cName

Yes

This is the name of the page. This will be how the page is referred to throughout the site, and will be automatically used in the page's <title> tag (unless overridden by a meta title attribute, which you can specify after the page has been created.)

cHandle

No

This is the handle of the page, that is, how the page title will appear in the page URL. If you omit this field, concrete5 will generate a handle based off of the cName field. This is a little confusing, because concrete5 allows dash characters ("-") in page handles, but does not seem to support them anywhere else. This is good, though, because the typical convention for page paths on the web is to use dashes rather than underscores.

cDescription

No

This is the internal description of the page. It is just a string that describes the purpose of a page. Some themes might use this data somewhere in the theme, but it is not required. Older versions of concrete5 displayed the description of the page in the dashboard, but that has gone away since the update to version 5.5.

uID

No

This is an integer ID to indicate the page's author. Pass in the ID of any user in the concrete5 Users table to set that user as the page author. If omitted, the currently logged in user will be registered as the page author.

pkgID

No

This is an integer ID of the package that created this page. Make sure to include this field if you are creating pages from a package. If you omit this field, but the attached page type belongs to a package, the page type's package ID will be attached to this page. This will help users optionally remove any pages that were created by your package if they choose to uninstall your package.

cDatePublic

No

This is a MySQL-formatted date string (YYYY-MM-DD) of when this page should be publicly visible on the website. If omitted, the current date and time from the web server is used.

cvIsApproved

No

This is a Boolean that indicates if the initial page version should be approved. Defaults to true if nothing is specified. When pages are approved, they are made immediately visible to all users that have permission to access that page on the website. Any subsequent edits to a page will require the new versions of the page to be approved before users can see the changes.

See also

  • The Updating an existing page recipe

  • The Getting a page type by its ID recipe

  • The Getting a page type by its handle recipe