Entity Translation for custom entity

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);
}
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Entity Translation for custom entity

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.