Problem/Motivation

I needed a way to add a preview column in the IEF table.

Proposed resolution

Allow to add a render callback for a cell in hook_inline_entity_form_table_fields_alter()

Comments

marcusx’s picture

Status: Active » Needs review
StatusFileSize
new1.85 KB

I had the same use case as #2287391: Add entity_view field type for theme_inline_entity_form_entity_table but saw this to late. I came up with a different solution. But here is the patch anyway. I would highly appreciate if at least one solution would make it into IEF.

Usage example

/**
 * Implements hook_inline_entity_form_table_fields_alter().
 *
 * @param $fields
 * @param $context
 */
function mymodule_inline_entity_form_table_fields_alter(&$fields, $context) {

  if ($context['parent_entity_type'] == 'node' && $context['entity_type'] == 'content_element') {

    // Add an entity preview column to the ief table.
    $fields['field_ief_preview'] = array(
      'type' => 'callback',
      'render_callback' => 'mymodule_ief_preview_column',
      'label' => t('Preview'),
      'weight' => 101,
    );

  }
}

/**
 * Callback providing a renderable array for an the ief table field (=column).
 * 
 * @param $entity
 * @return array()
 */
function mymodule_ief_preview_column($entity) {

  $output = entity_view('content_element', array($entity->id => $entity), $view_mode = 'ief_preview', NULL, FALSE);
  return $output;
}
axe312’s picture

StatusFileSize
new1.86 KB

I added the entity type as callback parameter to improve the support of rendering entities in the callback.

To simplify the usage, I created a module which implements a preview view mode and table column for every entity. You can find it here: https://www.drupal.org/project/inline_entity_form_preview

yannickoo’s picture

Thank you for sharing, nice thing!

  1. +++ b/inline_entity_form.api.php
    @@ -65,7 +65,7 @@ function hook_inline_entity_form_settings_alter(&$settings, $field, $instance) {
    + *   - type: either 'property', 'field' or 'callback' to specify how the data is defined on
    

    This description should break after 80 characters.

  2. +++ b/inline_entity_form.module
    @@ -1682,6 +1682,9 @@ function theme_inline_entity_form_entity_table($variables) {
    +      elseif ($field['type'] == 'callback' && isset($field['render_callback'])) {
    

    You will get a fatal error when the function does not exist in code.

I fixed that two issues in the attached patch.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Awesome, I closed my original issue and this approach seems to win out:) Either way we win, just putting focus one this issue!

Cheers and thanks for the code and the cleanups.

bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks.

  • bojanz committed 7b5e448 on 7.x-1.x authored by yannickoo
    Issue #2324901 by yannickoo, axe312, marcusx: Added callback field type...
axe312’s picture

Awesome, thank you :)

joelpittet’s picture

So much awesome!

joachim’s picture

This patch has invented a new callback. Callbacks should be documented in a similar way to hooks: see https://www.drupal.org/node/1354:#functions

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.