Scenario

I want to include the created property of my entity, in the Inline entity form - Multiple values widget. But I don't want to display the timestamp, I want to show a more readable date format, by using the format_date() function (or any other function for that matter).

Proposal

Add an option in the table fields (the array returned from the tableFields() controller function), that will allow a function to alter the output.
So a field definition could look like the following:

<?php
$fields
['created'] = array(
 
'type' => 'property',
 
'label' => t('Created'),
 
'weight' => 2,
 
'process' => 'format_date' // The new output altering function
);
?>

I don't know if process is the right word for this option, but it's what I've been using in my own altered IEF version.

Solution

Inject the function in the theme_inline_entity_form_entity_table(), before the actual data is added to the $cells array.

Comments

Birk’s picture

Status:Active» Needs review
StatusFileSize
new676 bytes

Instead of putting the call right before the data is added to the $cells array, I've added the call in the property condition.
This way the process option is only used on properties and not on fields, where changes like this should be done in the formatter.

cyborg_572’s picture

Issue summary:View changes
StatusFileSize
new1.37 KB
new1.15 KB

I found this very useful, but needed to be able to pass arguments to the formatter, so I extended the patch a little to support an array of arguments.

michfuer’s picture

+1 for usefulness.

I couldn't apply the patch in #2 (it's stale). Here's an updated one.

cyborg_572’s picture

The patch in #3 drops the ability to use an integer 1 to pass the property value as anything other than the first argument to the formatter (kind of like how hook_menu does it). I'm not sure if that's needed for anything other than what I was doing (which I can't remember now), but I just wanted to leave a heads up to anyone using the patch.

xlyz’s picture

#3 works perfectly here. Please commit.

xlyz’s picture

Priority:Minor» Normal
Status:Needs review» Reviewed & tested by the community
pjcdawkins’s picture

I'd prefer is_callable() rather than function_exists(), so that anonymous functions or object methods could be used.

pjcdawkins’s picture

Status:Reviewed & tested by the community» Needs review
StatusFileSize
new2.5 KB

A new patch, with the following modifications to #3:

  • Renamed 'formatter' to 'formatter callback', to distinguish this clearly from field formatters (which operate quite differently). Still, I wonder if 'property formatter' might be clearer.
  • Renamed 'arguments' to 'formatter arguments'.
  • Documented the new keys in EntityInlineEntityFormController::tableFields().
  • [tangential to this issue] Removed the undocumented 'sanitized' behavior, in favour of passing array('sanitize' => TRUE) to $property->value().
pjcdawkins’s picture

New patch. It turns out 'sanitize' was documented, but since Entity API handles sanitizing I don't understand why it should be there. I think it would be confusing DX to keep it. Also, the docblock in inline_entity_form.api.php (for the hook) needed harmonizing with the one in EntityInlineEntityFormController (for the method).

pjcdawkins’s picture

Sorry, missed out an array().

robertsm’s picture

Hello,

I have tried to use:

   $fields['calendar_date'] = array(
        'type'    => 'field',
        'label'   => t('Calendar Date'),
        'weight'  => 80,
        'formatter' => 'format_date',
        'settings' => array(
           'type' => 'custom_short_date',   //this is date format create in drupal front end
         );
    );

When I refresh a webpage nothing happened in IEF table. Any suggestion? When I debuged it with dpm($renderable_data) it shows me #formatter: list_default !