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.
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'));
}
}
}