Using t() inside hook_field_formatter_info() apparently doesn't work, as seen in #1211728: Labels untranslated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
2.88 KB
fago’s picture

Status: Needs review » Needs work

Actually I'm not sure how this should be handled. One should not pass dynamic variables into t(), however I think this is how core handles node type translation too. Then there is the i18n API for this, e.g. http://drupal.org/node/1114010

Maybe we should try to get input from someone with i18n skills?

Dave Reid’s picture

The settings in formatter_info should probably be something like 'edit' => TRUE, 'delete' => TRUE rather than the labels as the key values.

Then in the formatter_view itself, declare an array of the labels for $op:

$op_labels = array(
  'edit' => t('Edit'),
  'delete' => t('Delete'),
);

then output $op_labels[$op] where desired.

Dave Reid’s picture

Or something like:

function _field_collection_op_label($op) {
  switch ($op) {
    case 'add':
      return t('Add');
    case 'edit':
      return t('Edit');
    case 'delete':
      return t('Delete');
   }
}
Dave Reid’s picture

I rescind my suggestions - I wasn't completely aware of how this worked.

tim.plunkett’s picture

FileSize
62.73 KB

Here is the screenshot. For those not familiar with the module, you can configure which operations are available, and what their labels are.

873834.png

Gábor Hojtsy’s picture

I'm not sure what is the root of the problem here. Is it that hook_field_formatter_info() is called / cached in a different HTTP request and therefore might be cached in a different language? That could easily be a problem. Cached data where the cache key does not include the language code should not use t(). I'd produce the labels based on the keys when the UI is actually constructed, but with verbatim text like t('Edit'), so it is actually picked up by the translation engine properly.

tim.plunkett’s picture

So are you saying we should replace the textfields with checkboxes? That we can't have dynamic strings like that?

Gábor Hojtsy’s picture

@tim.plunkett: Anything that is user editable (except if its a node or a path alias or date format in D7), Drupal core DOES NOT provide ANY solution for its translation. if you want to have user customizable text to be translated the only solution right now is the Drupal 7 i18n_strings module under the i18n module suite. This will not be solved in other ways likely before Drupal 8.

fago’s picture

thanks Gabor - so it looks like we have to go with i18n for that.

Still, is it good practice to have translated defaults ala t('Edit') ?

tim.plunkett’s picture

Honestly, I'm not sure that the links should be editable in the GUI. I think hook_field_formatter_info_alter() is enough to expose the setting.

tim.plunkett’s picture

Category: bug » feature

If someone wants to work on i18n integration, feel free.

dolilmao’s picture

I'm trying to create a block to list all field collection items and "add" links to add the field collection item like following. I am very new to drupal can someone point me out to how i do this. I have tried to create a block and using the code that list add path in field collection module it does not work. Please help

Photo add
Video add

dolilmao’s picture

I'm trying to use the following code to list links to add field collection item is a block but i get a bunch of error printed on the page.

/**
 * Implements hook_block_info().
 */
function keku_features_block_info() {
  $menus = mb_features_menu_get_menus();

  $blocks = array();
  foreach ($menus as $name => $title) {
    $blocks[$name]['info'] = check_plain($title);
    // Menu blocks can't be cached because each menu item can have
    // a custom access callback. menu.inc manages its own caching.
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function keku_features_block_view($delta = '') {
  $data['subject'] = 'Add More Sections';
  $data['content'] = mb_features_menu_get_menus($entity_type, $entity);
  // Add contextual links for this block.
  if (!empty($data['content'])) {
    $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($delta));
  }
  return $data;
}
function keku_features_menu_get_menus($entity_type, $entity) {
module_load_include('module', 'field_collection');
  $element = array();
  $settings = $display['settings'];

    // Check whether the current is allowed to create a new item.
    $field_collection_item = entity_create('field_collection_item', array('field_name' => $field['field_name']));
    $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE);
if (field_collection_item_access('create', $field_collection_item)) {
foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
      $path = field_collection_field_get_path($field);
      list($id) = entity_extract_ids($entity_type, $entity);
      $element['#suffix'] = '';
      if (!empty($settings['description'])) {
        $element['#suffix'] .= '<div class="description field-collection-description">' . field_filter_xss($instance['description']) . '</div>';
      }
      $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_add", $settings['add']);
      $add_path = $path . '/add/' . $entity_type . '/' . $id;
      $element['#suffix'] .= '<ul class="action-links action-links-field-collection-add"><li>';
      $element['#suffix'] .= l($title, $add_path, array('query' => drupal_get_destination()));
      $element['#suffix'] .= '</li></ul>';
    }
  return $element;      
}
}

RobW’s picture

@dolilmao, That's a support request; you should make a new issue with your problem instead of tacking it on here. This issue is just for adding translation to user created add/edit/delete links to the field collection module. You might also look for help in #drupal-support on IRC.

Good luck.

jmuzz’s picture

Issue summary: View changes

I've made a start on this, and have some hooks that will store the values entered on the display settings form and allow translations to be added for them. I don't see a good way to have these translations be displayed. The problem is in the field_collection_field_formatter_view function where it should happen. It needs to use the name of the display mode as part of a string key for i18n_string(), since these links could potentially have different labels for each display mode. The hook does not have access to the name of the display mode as described here:

http://drupal.stackexchange.com/questions/39224/how-to-get-view-mode-fro...

This is what I have so far:

/**
 * Implements hook_i18n_string_info()
 */
function field_collection_i18n_string_info() {
  $groups['field_collection'] = array(
    'title' => t('Field Collection'),
    'description' => t('Add / Edit / Delete links for field collection items.'),
    'format' => FALSE,
    'list' => TRUE,
    //'refresh callback' => 'field_collection_i18n_string_refresh',
  );
  return $groups;
}

function field_collection_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'field_collection_display_settings_changed';
}

function field_collection_display_settings_changed($form, &$form_state) {
  foreach($form_state['input']['fields'] as $field_name => $field) {
    if (isset($form_state['formatter_settings'][$field_name]) && $field['type'] == 'field_collection_view' || $field['type'] == 'field_collection_list') {
      $string_key = "field_collection:formatter_link:" . $form['#entity_type'] . ":" .
                    $form['#bundle'] . ":" . $form['#view_mode'] . ":" .
                    $field_name . ":";
      foreach (array("add", "edit", "delete") as $link) {
        i18n_string_update($string_key . $link, $form_state['formatter_settings'][$field_name][$link]);
      }
    }
  }
}