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

Installing the block type with the package


Right now, if we were to install the package, the block type would not come with it, and users of your add-on would be confused and disappointed. We can tell the package to install the block type during the package's installation, though.

Open the package controller (/packages/cookbook_gallery/controller.php) in your code editor.

Add a new method to the controller class called install, as follows:

public function install() {
  $pkg = parent::install();

  // install the block type
  BlockType::installBlockTypeFromPackage('cookbook_gallery', $pkg); 
}

What's going on here? Well, the Package class has a function called install, which, you guessed it, installs the package to the concrete5 database. Since our package controller extends the Package class, the install function is automatically available to us.

The first step is to call the Package class's install function and get the object that it returns. This will allow us to install the block type and have it assigned to our package.

It's important to pass the $pkg object to the block type installer, because if users uninstall or reinstall your add-on, the block type will come and go with it.

Save the controller file. It's time to install our add-on!

Installing the package in concrete5

Visit /dashboard/extend/install/ on your concrete5 site (you'll have to log in if your haven't already). You will see your add-on awaiting installation.

The gallery add-on awaiting installation is as shown in the following screenshot:

Click on the Install button. concrete5 will add your package and its block controller to the site. If it succeeds, you will see a message at the top of the screen.

After successful installation of the add-on, we see the following screenshot:

Creating a page for the gallery

Visit your concrete5 homepage and hover over the Edit button to add a sub-page underneath the home page.

Let's use the Full layout for this gallery page.

Give the page a title and path. How about Image Gallery?

Add the page to the site. Now that we have a nice place to put our block, let's actually make our block do something!