Hi,

I was listing ECK "Rendered Entity" via Views module. That calls the entity_view.

My next problem after programmatically creating the full entity display was that properties are not shown.
I am using field_extra_fields to map properties that implement a "default_formatter" and rearrange them in entity display, and that works in the entity page.

From what I see, the problem is that properties are shown on the entity page as an implementation of hook_menu (eck__entity__view function). But they are not attached to the entity view via the entity controller, or in a entity_view function.

What I ended up doing was to implement entity_view. This is overkill at least because the process of attaching properties is done twice when viewing entities via hook_menu. See below:

/**
 * Implements hook_entity_view().
 */
function eckextend_entity_view($entity, $type, $view_mode, $langcode) {
  // Not sure if we should do it like this. There is overhead with ECK view.
  // Act only for ECK entities.
  $eck_entity_types = eckextend_get_eck_entity_types();
  if (array_key_exists($type, $eck_entity_types)) {
    // Get all entity properties and attach properties to entity content.
    $entity_type = entity_type_load($type);
    $property_view = array();
    $formatters = eck_property_behavior_invoke_plugin($entity_type, 'default_formatter',
      array('entity' => $entity));
    foreach ($formatters as $property => $formatter) {
      $property_view[$property] = $formatter;
    }
    eck_property_behavior_invoke_plugin($entity_type, 'entity_view',
      array('entity' => $entity));

    // Add property view in the same way as eck__entity__view() to avoid double
    // adding the properties on entity page.
    $entity->content = array_merge($property_view, $entity->content);
  }
}

From my standpoint it would be ideal to attach properties in the entity view and all that eck__entity__view would do is to call entity_view.

Am I missing something?
Please let me know.
Thanks,
Mihai

Comments

fmizzell’s picture

@mihai_brb I don't see any issues with your solution. It all depends on what we would like the canonical source of the renderable array for an entity to be. Currently in 7.x-2.x, the source is eck__entity__view, so it would also make sense to have something like this:

function eck_entity_view($entity, $type, $view_mode, $langcode) {
  $eck_entity_types = eckextend_get_eck_entity_types();
  if (array_key_exists($type, $eck_entity_types)) {
    return eck__entity__view($type, $entity->bundle(), $entity);
  }
}

Thinking to the future, I think that eventually the code will turn into something like this:

function eck_entity_view($entity, $type, $view_mode, $langcode) {
  $eck_entity_types = eckextend_get_eck_entity_types();
  if (array_key_exists($type, $eck_entity_types)) {
    return $entity->view();
  }
}
mihai_brb’s picture

@fmizzell
This will cause an infinite loop:
return eck__entity__view($type, $entity->bundle(), $entity);

Because there is call for entity_view (via eck__entity__build) in eck__entity__view.
That is why I was thinking that eck__entity__view should call entity_view, not the other way around.

Thanks for your feedback!
Mihai

fmizzell’s picture

@mihai_brb Ha, I guess you know my code better than I do :)

mihai_brb’s picture

Status: Active » Closed (duplicate)
hgoto’s picture

I was stuck with the property display issue for hours... Though this is a fixed and closed issue, I'd like to leave a comment for people who visit this page and may have a same question as I had.

There are 7 default property behaviors in ECK by default.

1. Author
2. Changed
3. Created
4. Language
5. Title and Formatter
6. Title
7. UUID

And among them, only "Title and Formatter" has the default formatter. Other formatters don't. It means, only "Title and Formatter" can be shown in the page. Other property behaviors such as "Author" and "Changed" cannot be shown on page even if you change the entity display settings. I was tricked before seeing the code.

If you want to create a custom property behavior and display it on page, you need to define "default_formatter" in that plugin definition. The "title_and_formatter.inc" file in ECK module can be a help when creating custom one.

Mykola Dolynskyi’s picture

@hgoto thank! Your comment was useful - i also was stuck for hours :)
P.S. Dont you know can i use EntityReference widget for field like "author" ?

hgoto’s picture

@Mykola Dolynskyi, thanks. As for the widget, I don't think we can use EntityReference widget with the version 7.x-2.x at the moment. But there's a roadmap and "Entity reference property" seems to be planned. There's a chance you can use it in the future :)

#2382255: ECK D7, D8 roadmap