[Tools] Create EntityTranslationGet tool

Created on 8 October 2025, 3 days ago

Tool details:

  • Label: Entity Translation Get
  • Description: Load or create a translated version of an entity for a specific language.
  • Class name: EntityTranslationGet
  • Plugin id: entity_translation_get
  • Module: tool:tool_content_translation

Tool inputs:

  • Entity(entity)(entity)(required)
  • Langcode (string)(string)(required)
  • Field Name (field_name)(string)(required)

Tool outputs:

  • Translated Entity (translated_entity)(entity)(required)

doExecute()

 ['entity' => $entity, 'langcode' => $langcode] = $values;

    // Validate that the entity is a content entity.
    if (!($entity instanceof ContentEntityInterface)) {
      return ExecutableResult::failure($this->t('The provided entity must be a content entity.'));
    }

    // Validate that the language code exists.
    $language = $this->languageManager->getLanguage($langcode);
    if (!$language) {
      return ExecutableResult::failure($this->t('Invalid language code "@langcode".', ['@langcode' => $langcode]));
    }

    // Check if the entity type supports translation.
    if (!$this->contentTranslationManager->isEnabled($entity->getEntityTypeId(), $entity->bundle())) {
      return ExecutableResult::failure($this->t('Translation is not enabled for @type @bundle entities.', [
        '@type' => $entity->getEntityTypeId(),
        '@bundle' => $entity->bundle(),
      ]));
    }

    try {
      // Check if translation already exists.
      if ($entity->hasTranslation($langcode)) {
        $translated_entity = $entity->getTranslation($langcode);
        return ExecutableResult::success($this->t('Successfully loaded existing @langcode translation of @type entity @id.', [
          '@langcode' => $langcode,
          '@type' => $entity->getEntityTypeId(),
          '@id' => $entity->id(),
        ]), ['translated_entity' => $translated_entity]);
      }

      // Create a new translation.
      $translated_entity = $entity->addTranslation($langcode, $entity->toArray());

      // Save the entity to persist the new translation.
      $translated_entity->save();

      return ExecutableResult::success($this->t('Successfully created and loaded new @langcode translation of @type entity @id.', [
        '@langcode' => $langcode,
        '@type' => $entity->getEntityTypeId(),
        '@id' => $entity->id(),
      ]), ['translated_entity' => $translated_entity]);

    } catch (\Exception $e) {
      return ExecutableResult::failure($this->t('Error processing translation for @type entity @id: @message', [
        '@type' => $entity->getEntityTypeId(),
        '@id' => $entity->id(),
        '@message' => $e->getMessage(),
      ]));
    }

checkAccess()

TBD.

πŸ“Œ Task
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States michaellander

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024