1

I have a custom entity and i don't have any fields in the entity.Instead what i have is:- Adding custom entity page has a form where we have several fields where we are able to fill all the fields in the form.Now the problem which i m facing is is: how to display the entity added on my_entity/% page?

1
  • check if this module helps you. – arpitr Sep 30 '13 at 11:31
1

if you are using Entity API, you can take a look to this article http://www.trellon.com/content/blog/creating-own-entities-entity-api and to the example_task module:

The process works like this:

  // Your entity view path
  $task_uri = 'task/%example_task';
  $task_uri_argument_position = 1;

  $items[$task_uri] = array(
    'title callback' => 'entity_label',
    'title arguments' => array('example_task', $task_uri_argument_position),
    'page callback' => 'example_task_view',
    'page arguments' => array($task_uri_argument_position),
    'access callback' => 'entity_access',
    'access arguments' => array('view', 'example_task', $task_uri_argument_position),
    'file' => 'example_task.pages.inc',
  );

/**
 * Task view callback.
 */
function example_task_view($task) {
  drupal_set_title(entity_label('example_task', $task));
  return entity_view('example_task', array(entity_id('example_task', $task) => $task), 'full');
}

This will invoke to the function in entity/includes/entity.controller.inc folder of Entity API module:

  /**
   * Implements EntityAPIControllerInterface.
   */
  public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
    // For Field API and entity_prepare_view, the entities have to be keyed by
    // (numeric) id.
    $entities = entity_key_array_by_property($entities, $this->idKey);
    if (!empty($this->entityInfo['fieldable'])) {
      field_attach_prepare_view($this->entityType, $entities, $view_mode);
    }
    entity_prepare_view($this->entityType, $entities);
    $langcode = isset($langcode) ? $langcode : $GLOBALS['language_content']->language;

    $view = array();
    foreach ($entities as $entity) {
      $build = entity_build_content($this->entityType, $entity, $view_mode, $langcode);
      $build += array(
        // If the entity type provides an implementation, use this instead the
        // generic one.
        // @see template_preprocess_entity()
        '#theme' => 'entity',
        '#entity_type' => $this->entityType,
        '#entity' => $entity,
        '#view_mode' => $view_mode,
        '#language' => $langcode,
        '#page' => $page,
      );
      // Allow modules to modify the structured entity.
      drupal_alter(array($this->entityType . '_view', 'entity_view'), $build, $this->entityType);
      $key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
      $view[$this->entityType][$key] = $build;
    }
    return $view;
  }
}

As you can see the content is finally rendered through a render element using "Entity.tpl.php"

A way to add your customizations to the view in order to render elements which are not fields may be extending from "EntityAPIController" and writing builContent function:

class ExampleTaskController extends EntityAPIController {

      public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
        $wrapper = entity_metadata_wrapper('example_task', $entity);
        $content['author'] = array('#markup' => t('Created by: !author', array('!author' => $wrapper->uid->name->value(array('sanitize' => TRUE)))));

        // Make Description and Status themed like default fields.
        $content['description'] = array(
          '#theme' => 'field',
          '#weight' => 0,
          '#title' =>t('Description'),
          '#access' => TRUE,
          '#label_display' => 'above',
          '#view_mode' => 'full',
          '#language' => LANGUAGE_NONE,
          '#field_name' => 'field_fake_description',
          '#field_type' => 'text',
          '#entity_type' => 'example_task',
          '#bundle' => $entity->type,
          '#items' => array(array('value' => $entity->description)),
          '#formatter' => 'text_default',
          0 => array('#markup' => check_plain($entity->description))
        );

        return parent::buildContent($entity, $view_mode, $langcode, $content);
      }
    }
2
  • i don't have fields in the entity. i have fields in form. how to render entity.? – neetu morwani Sep 30 '13 at 11:34
  • 1
    I have edited the answer. I think what you are looking for is extending builContent function in roder to show your custom info which is not a drupal field. Hope this helps – Enxebre Sep 30 '13 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.