2

I'm Developing a custom module with custom content entities that needs to be translatable. The problem is that when they are made translatable by adding: translatable = TRUE in the @ContentEntityType annotation, the entities appear twice in the drop down view setting options (check the image, 'Faculty' appears twice) which is really strange.

Custom entities appear twice when creating views

I don't mind if they show twice or more than that but the problem comes when you decide which to choose to create your view because one of the options results in only the entity label field(name) available for building the view, of course along with other unnecessary fields to my use caes such as UUID, created, changed, ..etc . However, the other option has the other fields that I added to the entity through the Manage fields UI and those are the ones I need.

I have done what this page at drupal.org stated { if the 'translatable' annotation has value TRUE} define a class that extends \Drupal\content_translation\ContentTranslationHandler, to translate the content.

There is no handy resource to learn how to impelement the TranslationHandeler class. So I have followed the one used in core of 'Taxanomy' entity which basically looks like it would work with any other entity. You just need to rename it! - correct me if I'm worng! Also, by the way translation of the entity works fine even in instances where I didn't define the TranslationHandaler class and I just included 'translatable = TRUE' annotation, which also is strange!!

I have tried every thing I could to fix it and researched for hours but with no luck. The two options dissappears if you don't include translatable = TRUE in the annotation but then you can not translate the entity fields this way.

My drupal version is 8.1.0 and I have used drupal console on windows to generate the module and the entities. Then I added the extra code.

Anyone could help please!

Code for defining TranslationHandeler class

namespace Drupal\registerar_office;

use Drupal\Core\Entity\EntityInterface;
use Drupal\content_translation\ContentTranslationHandler;
use Drupal\Core\Form\FormStateInterface;

 /**
 * Defines the translation handler for faculty.
 */
class FacultyTranslationHandler extends ContentTranslationHandler {

 /**
  * {@inheritdoc}
  */
  public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
    parent::entityFormAlter($form, $form_state, $entity);
    $form['actions']['submit']['#submit'][] = array($this, 'entityFormSave');
  }

  /**
   * Form submission handler for TermTranslationHandler::entityFormAlter().
   *
   * This handles the save action.
   *
   * @see \Drupal\Core\Entity\EntityForm::build().
   */
  function entityFormSave(array $form, FormStateInterface $form_state) {
    if ($this->getSourceLangcode($form_state)) {
      $entity = $form_state->getFormObject()->getEntity();
      // We need a redirect here, otherwise we would get an access denied page,
      // since the current URL would be preserved and we would try to add a
      // translation for a language that already has a translation.
      $form_state->setRedirectUrl($entity->urlInfo('edit-form'));
    }
  }

}
1

I don't think it's directly associated with translation handling - it looks like it's the distinction between the entity and entity_field_data table. The "working" option in the dropdown is a class derived from WizardPluginBase.

I fixed my problem by making sure both of these were in my Drupal\custom_entity\Entity\CustomEntityViewsData::getViewsData

$data['custom_entity']['table']['wizard_id'] = 'custom_entity';
$data['custom_entity_field_data']['table']['wizard_id'] = 'custom_entity';`

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.