3

I've created a custom entity and in the next step, I would like to use the Entity Translation module to make the entity translatable.

My entity is defined like this

/**
 * Implements hook_entity_info().
 */
function MY_MODULE_entity_info() {
  $info['custom_entity'] = array(
    'label' => t('Custom Entity'),
    'base table' => 'custom_entity',
    'module' => 'MY_MODULE',
    'uri callback' => 'custom_entity_basic_uri',
    'controller class' => 'EntityCustomController',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'qid',
      'label' => 'title',
    ),
    'translation' => array(
      'entity_translation' => array(
        'default settings' => array(
          'default_language' => 'de',
        ),
        'base path' => 'my-path/%custom_entity',
        'view path' => 'my-path/%custom_entity',
        'edit path' => 'admin/my-path/%custom_entity/edit',
        'translate path' => 'my-path/%custom_entity/translate',
        'path wildcard' => '%custom_entity',
      ),
    ),
    'static cache' => FALSE,
    'bundles' => array(
      'custom_entity' => array(
        'label' => 'Custom bundle',
        'admin' => array(
          'path' => 'admin/structure/custom/manage',
          'access arguments' => array('administer custom entities'),
        ),
      ),
    ),
    'view modes' => array(
      'full' => array(
        'label' => t('Full'),
        'custom settings' => FALSE,
      ),
    ),
  );
  return $info;
}

With this definition, my custom entity appears in the Entity Translation admin view (admin/config/regional/entity_translation) and I can configure it. This seems to be correct, because in the 'manage field' tab, the language selector field appears and the 'translate' tab appears also in the edit form.

The only strange thing I found is that the language selector is not rendered in the edit form in any time. If I save the translation, the source entity is overriden with the changes, but no translation is added.

I thought the problem is my Controller class, but this seems to work correctly too:

class EntityCustomController extends EntityAPIControllerExportable {
  public function create(array $values = array()) {
    $values += array(
      'created' => REQUEST_TIME,
      'type' => 'custom_entity',
      'qid' => 0,
      'bundle_type' => 'custom_entity',
      'title' => '',
    );
    return parent::create($values);
  }

  /**
   * Save Task Type.
   */
  public function save($entity, DatabaseTransaction $transaction = NULL) {
    parent::save($entity, $transaction);
  }

}

In my custom edit form, I use field_attach_form function to render all the defined themes.

And, if it helps, this is my save handler:

/**
 * Save entity by using the controller.
 */
function MY_MODULE_custom_entity_save(&$entity) {
  return entity_get_controller('custom_entity')->save($entity);
}
0

In my case the issue ended up being pretty simple to fix. I found that the function entity_translation_form_alter() was testing to see if my entity's edit form was suitable for alteration. My edit form failed the very first test because entity_translation_edit_form_info() was returning false.

entity_translation_edit_form_info() was returning false because my form didn't have $form_state[$entity_info['translation']['entity_translation']['edit form']] set and set to the entity I was trying to edit.

tl;dr: Make sure you set a value in your edit form's $form_state at the path you declare in the edit form element of the settings array for your entity returned by your implementation of hook_entity_info().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.