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:

$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.

Files: 
CommentFileSizeAuthor
#15 inline_entity_form-entity_property_formatter-2097583-15.patch4.33 KBkevineinarsson
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View
#10 inline_entity_form-entity_property_formatter-2097583-10.patch4.06 KBpjcdawkins
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch inline_entity_form-entity_property_formatter-2097583-10.patch. Unable to apply patch. See the log in the details link for more information. View
#3 inline_entity_form-entity_property_formatter-2097583-3.patch1.66 KBmichfuer
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View
#2 interdiff-2097583-1-2.txt1.15 KBcyborg_572
#2 ief-table_property_process-2097583-2.patch1.37 KBcyborg_572
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch ief-table_property_process-2097583-2.patch. Unable to apply patch. See the log in the details link for more information. View
#1 2097583-formatting-property-fields.patch676 bytesBirk
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 2097583-formatting-property-fields.patch. Unable to apply patch. See the log in the details link for more information. View

Comments

Birk’s picture

Status: Active » Needs review
FileSize
676 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 2097583-formatting-property-fields.patch. Unable to apply patch. See the log in the details link for more information. View

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
FileSize
1.37 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch ief-table_property_process-2097583-2.patch. Unable to apply patch. See the log in the details link for more information. View
1.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

FileSize
1.66 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View

+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
FileSize
2.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

FileSize
4.06 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch inline_entity_form-entity_property_formatter-2097583-10.patch. Unable to apply patch. See the log in the details link for more information. View

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 !

sgurlt’s picture

Hey, for all that come to this problem with date formats, this must look like this:

    $fields['field_perf_date_and_time'] = array(
      'type' => 'field',
      'label' => t('Date and Time'),
      'weight' => 5,
      'formatter' => 'date_format',
      'settings' => array(
        'format_type' => 'short'
      )
    );
karolrybak’s picture

Hi I'm using the following code to display node created date. However it is still formatted as timestamp.
Also tried some other combinations though nothing seems to work. Any help is appreciated.

       $fields['created'] = array(
            'type' => 'property',
            'label' => 'Date',
            'weight' => 103,
            'formatter' => 'date_format',
            'settings' => array(
              'format_type' => 'short'
            )
        );
kristofferwiklund’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch does not apply for latest dev version.

kevineinarsson’s picture

FileSize
4.33 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View

Patch rerolled so it applies to 7.x-1.x.

kevineinarsson’s picture

Status: Needs work » Needs review
kevineinarsson’s picture

Issue tags: -Needs reroll +Needs Review

The last submitted patch, 1: 2097583-formatting-property-fields.patch, failed testing.

The last submitted patch, 2: ief-table_property_process-2097583-2.patch, failed testing.

kristofferwiklund’s picture

Status: Needs review » Reviewed & tested by the community

Patch solves problem when displaying "created" date field in inline.

Ruslan P’s picture

Thank you, Kristoffer.
I am confirming, your path works for me as well.

    $fields['expires'] = array(
      'type' => 'property',
      'label' => t('Expires'),
      'weight' => 2,
      'formatter callback' => 'format_date',
      'formatter arguments' => array(
        'custom',
        'Y-m-d H:i',
      ),
    );