You are here

function tmgmt_content_tmgmt_source_suggestions in Translation Management Tool 8

Implements hook_tmgmt_source_suggestions().

File

sources/content/tmgmt_content.module, line 26
Source plugin for the Translation Management system that handles entities.

Code

function tmgmt_content_tmgmt_source_suggestions(array $items, JobInterface $job) {
  $suggestions = array();
  foreach ($items as $item) {
    if ($item instanceof JobItemInterface && $item
      ->getPlugin() == 'content') {

      // Load the entity, skip if it can't be loaded.
      $entity = ContentEntitySource::load($item
        ->getItemType(), $item
        ->getItemId(), $job
        ->getSourceLangcode());
      if (!$entity || !$entity instanceof ContentEntityInterface) {
        continue;
      }

      // Load translatable menu items.

      /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
      $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
      $entity_type = $entity
        ->getEntityType()
        ->id();
      $menu_items = $menu_link_manager
        ->loadLinksByRoute('entity.' . $entity_type . '.canonical', [
        $entity_type => $entity
          ->id(),
      ]);
      if (!empty($menu_items)) {
        foreach ($menu_items as $menu_item) {

          /** @var \Drupal\menu_link_content\MenuLinkContentInterface $target */
          $target = \Drupal::service('entity.repository')
            ->loadEntityByUuid($menu_item
            ->getBaseId(), $menu_item
            ->getDerivativeId());
          if ($target
            ->hasTranslation($job
            ->getSourceLangcode())) {
            $suggestions[] = [
              'job_item' => tmgmt_job_item_create('content', $menu_item
                ->getBaseId(), $target
                ->id()),
              'reason' => t('Menu item @label', [
                '@label' => $target
                  ->label(),
              ]),
              'from_item' => $item
                ->id(),
            ];
          }
        }
      }
      $embedded_fields = ContentEntitySource::getEmbeddableFields($entity);

      // Loop over all fields, check if they are NOT translatable. Only if a
      // field is not translatable we may suggest a referenced entity.
      $content_translation_manager = \Drupal::service('content_translation.manager');
      foreach ($entity as $field) {

        /* @var \Drupal\Core\Field\FieldItemListInterface $field */
        $definition = $field
          ->getFieldDefinition();

        // Skip fields that are already embedded.
        if (isset($embedded_fields[$definition
          ->getName()])) {
          continue;
        }

        // Loop over all field items.
        foreach ($field as $field_item) {

          // Loop over all properties of a field item.
          foreach ($field_item
            ->getProperties(TRUE) as $property) {
            if ($property instanceof EntityReference && ($target = $property
              ->getValue())) {
              $enabled = $content_translation_manager
                ->isEnabled($target
                ->getEntityTypeId(), $target
                ->bundle());
              if ($enabled && $target
                ->hasTranslation($job
                ->getSourceLangcode())) {

                // Add the translation as a suggestion.
                $suggestions[] = array(
                  'job_item' => tmgmt_job_item_create('content', $target
                    ->getEntityTypeId(), $target
                    ->id()),
                  'reason' => t('Field @label', array(
                    '@label' => $definition
                      ->getLabel(),
                  )),
                  'from_item' => $item
                    ->id(),
                );
              }
            }
          }
        }
      }
    }
  }
  return $suggestions;
}