Build Your Own Custom Entities in Drupal – Implementation

Posted by Unknown on Friday, May 16, 2014

Welcome back to the second part of this tutorial in which we explore the world of custom entities in Drupal. If you haven’t already, I strongly recommend you read the first installment, but let’s do a short recap nonetheless.


In the previous article we’ve defined the schema for our entity type and registered it with Drupal. We’ve also overridden the EntityAPIController to build up the display for our entities.


In this part of the tutorial we will continue and talk about a few other cool things we can do with entities in Drupal. First, we’ll quickly set up the pages where we can display the individual project entities. Next, we will build a straightforward but very powerful admin interface to manage them. Then, we will make our entity type fieldable so we can add fields through the UI. And finally, we’ll expose it to Views so we can create proper listings of project entities.


If you want, you can follow along with the source code from the first branch of the Git repository, or take a peek into the second branch which contains all the code we will cover today.


Individual entity pages


The first thing we’ll do is create the pages for displaying individual project entities. We’ll start by adding a new item to our hook_menu() implementation:



$items['project/%'] = array(
'title' => 'Project',
'page callback' => 'demo_view_project',
'page arguments' => array(1),
'access arguments' => array('access content'),
);


We are registering a path (project/id) and a callback function (demo_view_project()) to which we pass the wildcard URL argument (the ID of the project). As for access, anybody with the access content permission can see the page.


Next, let’s write the said callback function (keep in mind this is a simple example just for demonstration purposes):



/**
* Callback function for displaying the individual project page
*/
function demo_view_project($id) {

$projects = entity_load('project', array($id));
$project = $projects[$id];

drupal_set_title($project->name);
$output = entity_view('project', array($project));

return $output;

}


Continue reading %Build Your Own Custom Entities in Drupal – Implementation%




more

{ 0 comments... » Build Your Own Custom Entities in Drupal – Implementation read them below or add one }

Post a Comment

Popular Posts