Hi,

wouldn't it be useful to write a basic guide or stub aimed at module developers explaining how to easily add diff functionality to their module, especially if their module is field based?

I wrote a simple module (still in sandbox) and it is not included in the diff panel if changes occur.
How do I add this to my module, or must this functionality be added to the node.inc?

Any help would be greatful.

ps: If anyone want to test the simple phone field module I wrote: http://drupal.org/sandbox/tecjam/1320886

Comments

alan d.’s picture

Currently, fields are not handled by the module. While it is possible, I wouldn't recommend it at the moment. I'm hoping to get #1365750: Generalize API and Integrate with core field types pushed into 7.x-2.x and then it is as simple as:

1) Create the file "simple_phone_field.diff.inc"

2) Implement the hook

/**
 * Diff field callback for parsing phone field comparative values.
 */
function simple_phone_field_field_diff_view($items, $context) {
  $diff_items = array();
  foreach ($items as $delta => $item) {
    $t_args = array(
      '@type' => $item['type'],
      '@country' => $item['country'],
      '@area' => _format_area_code($item['area']),
      '@number' => $item['number'],
      '@description' => $item['description'],
    );
    if(!empty($item['description'])) {
      $diff_items[$delta] =  t('@type: @country / @area / @number [@description]', $t_args);
    }
    else {
      $diff_items[$delta] = t('@type: @country / @area / @number', $t_args);
    }
  }

  return $diff_items;
}

However, you can fake this functionality in 7.x-1.x by populating the field property 'value' on load. Use the same logic for generating the value property as above.

/**
 * Implements hook_field_load().
 */
function simple_phone_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items) {
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      $items[$id][$delta]['value'] = simple_phone_field_safe_value($item);
    }
  }
}

/**
 * Helper function to generate a safe value for the diff module.
 */
function simple_phone_field_safe_value($item) {
    $t_args = array(
      '@type' => $item['type'],
      '@country' => $item['country'],
      '@area' => _format_area_code($item['area']),
      '@number' => $item['number'],
      '@description' => $item['description'],
    );
    if(!empty($item['description'])) {
      return t('@type: @country / @area / @number [@description]', $t_args);
    }
    else {
      return t('@type: @country / @area / @number', $t_args);
    }
}
tecjam’s picture

That's a great starting point. Thanks for your input.

alan d.’s picture

The initial example is now usable with the new branch, 7.x-3.0-alpha1 or 7.x-3.0-dev.

This info can be found in the readme / api / core field examples.

alan d.’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

link to module