Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Override the Entity Controller for Better Rendering and Saving

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:02
    [Entity API and Drupal 7, Chapter 9, with Joe Shindelar]
  • 0:05
    We've now got the ability for someone to create a new video entity
  • 0:09
    by filling out a form, and a whole administrative UI to go with it.
  • 0:12
    We've also got the ability to navigate
  • 0:15
    to a URL for our entity.
  • 0:18
    But right now, all we see when we go there is the title of the entity.
  • 0:21
    What I'd like to do in this lesson
  • 0:24
    is take a look at the steps that are required
  • 0:26
    in order to render the rest of the properties on our entity.
  • 0:29
    We're going to override the entity controller

Override the Entity Controller for Better Rendering and Saving


So far we’ve seen that you can use entity_load and entity_view to display your entity. But what if you want more control over how your content gets rendered? In this lesson we’ll take a look at overriding the buildContent() method of our entity controller to spruce up the output a bit. We'll also overrid the save() method of the controller in order to populate the created_at and updated_at fields automatically when saving a video entity.

Note: In order to improve upon the security of the code in this lesson you'll want to make sure you're escaping all user input properly. The following code:

$build['embedcode'] = array(
'#type' => 'markup',
'#markup' => '<iframe width="560" height="315" src="'. $entity->embedcode . '" frameborder="0" allowfullscreen></iframe>',

Should be updated to use the check_url() function when outputting $entity->embedcode to ensure that user entered content is safe for use in the context of a URL.

$build['embedcode'] = array(
'#type' => 'markup',
'#markup' => '<iframe width="560" height="315" src="'. check_url($entity->embedcode) . '" frameborder="0" allowfullscreen></iframe>',

Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.