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

Getting a Page object by its ID


In many situations, you will need to load a Page object using its unique numerical ID from the database. As mentioned earlier, pages are actually extensions of the Collection object, so you will actually be loading the page by its collection ID.

Getting ready

We will need to know the numerical ID of the page that you are attempting to load. Often this will be stored in other objects, perhaps even a custom entity that you have stored in the database. For example, say you have a recipes table in the database, and each recipe has its own page in the concrete5 website. You store the page ID for each recipe, so you can load the relevant Page object whenever necessary.

How to do it...

First you will have to get the page ID for the Page object that you are trying to load. For this example, we will just hard-code the ID of the page that we are loading. The steps are as follows:

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

  2. First, let's store the ID of the page we are loading into a variable. In real-world use, you will likely have this ID stored in a database table, or available from some other code that you have run. The ID of the home page in concrete5 defaults to 1, so we will use that for our page's ID:

    $pageId = 1; // load the home page
  3. Now that we have the ID of the page that we need to load, use the getByID function to get the Page object that we need:

    $page = Page::getByID($pageId);
  4. We can inspect the $page variable and verify that it contains a Page object.

    my_debug($page);

How it works...

The page ID (also known as collection ID) is unique in the database, so calling the getByID function will load the correct page data and return a Page object that you can interact with.

See also

  • The Getting the current Page object recipe

  • The Getting a Page object by its path recipe