View source
<?php
namespace Drupal\lingotek\Form;
use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\lingotek\LanguageLocaleMapperInterface;
use Drupal\lingotek\LingotekConfigurationServiceInterface;
use Drupal\lingotek\LingotekContentTranslationServiceInterface;
use Drupal\lingotek\LingotekInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LingotekManagementRelatedEntitiesForm extends LingotekManagementFormBase {
protected $node;
public function __construct(Connection $connection, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, LingotekInterface $lingotek, LingotekConfigurationServiceInterface $lingotek_configuration, LanguageLocaleMapperInterface $language_locale_mapper, ContentTranslationManagerInterface $content_translation_manager, LingotekContentTranslationServiceInterface $translation_service, PrivateTempStoreFactory $temp_store_factory, StateInterface $state, ModuleHandlerInterface $module_handler, EntityFieldManagerInterface $entity_field_manager = NULL, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, LinkGeneratorInterface $link_generator = NULL) {
parent::__construct($connection, $entity_type_manager, $language_manager, $lingotek, $lingotek_configuration, $language_locale_mapper, $content_translation_manager, $translation_service, $temp_store_factory, $state, $module_handler, NULL, $entity_field_manager, $entity_type_bundle_info, $link_generator);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('entity_type.manager'), $container
->get('language_manager'), $container
->get('lingotek'), $container
->get('lingotek.configuration'), $container
->get('lingotek.language_locale_mapper'), $container
->get('content_translation.manager'), $container
->get('lingotek.content_translation'), $container
->get('tempstore.private'), $container
->get('state'), $container
->get('module_handler'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'), $container
->get('link_generator'));
}
public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $node = NULL) {
$this->node = $node;
$form = parent::buildForm($form, $form_state);
$depth = $this
->getRecursionDepth();
$form['depth_selection'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'form-item-depth-selection',
],
'#weight' => 60,
];
$form['depth_selection']['depth'] = [
'#type' => 'select',
'#title' => $this
->t('Recursion depth:'),
'#options' => [
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
],
'#default_value' => $depth,
];
$form['depth_selection']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply'),
'#submit' => [
[
$this,
'recursionDepthCallback',
],
],
];
return $form;
}
protected function getSelectedEntities($values) {
$entityTypes = [];
$entities = [];
foreach ($values as $type_entity_id) {
list($type, $entity_id) = explode(":", $type_entity_id);
$entityTypes[$type][] = $entity_id;
}
foreach ($entityTypes as $type => $values) {
$entities = array_merge($entities, $this->entityTypeManager
->getStorage($type)
->loadMultiple($values));
}
return $entities;
}
public function getFormId() {
return 'lingotek_entity_management';
}
protected function getHeaders() {
$headers = [
'label' => $this
->t('Label'),
'entity_type_id' => $this
->t('Content Type'),
'bundle' => $this
->t('Bundle'),
'source' => $this
->t('Source'),
'translations' => $this
->t('Translations'),
'profile' => $this
->t('Profile'),
'job_id' => $this
->t('Job ID'),
];
return $headers;
}
public function calculateNestedEntities(ContentEntityInterface &$entity, &$visited = [], &$entities = []) {
$visited[$entity
->bundle()][] = $entity
->id();
$entities[$entity
->getEntityTypeId()][] = $entity;
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
foreach ($field_definitions as $k => $definition) {
$field_type = $field_definitions[$k]
->getType();
if ($field_type === 'entity_reference' || $field_type === 'er_viewmode' || $field_type === 'entity_reference_revisions') {
$target_entity_type_id = $field_definitions[$k]
->getFieldStorageDefinition()
->getSetting('target_type');
$target_entity_type = $this->entityTypeManager
->getDefinition($target_entity_type_id);
if ($target_entity_type instanceof ContentEntityType) {
foreach ($entity->{$k} as $field_item) {
if (!isset($entities[$target_entity_type_id])) {
$entities[$target_entity_type_id] = [];
}
$entities[$target_entity_type_id][] = $field_item->target_id;
}
}
}
}
return $entities;
}
protected function getNestedEntities(ContentEntityInterface &$entity, &$visited = [], &$entities = [], $depth = 1) {
$visited[$entity
->bundle()][] = $entity
->id();
$entities[$entity
->getEntityTypeId()][$entity
->id()] = $entity
->getUntranslated();
if ($depth > 0) {
--$depth;
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
foreach ($field_definitions as $k => $definition) {
$field_type = $field_definitions[$k]
->getType();
if (in_array($field_type, [
'entity_reference',
'entity_reference_revisions',
'er_viewmode',
])) {
$target_entity_type_id = $field_definitions[$k]
->getFieldStorageDefinition()
->getSetting('target_type');
$target_entity_type = $this->entityTypeManager
->getDefinition($target_entity_type_id);
if ($target_entity_type instanceof ContentEntityType) {
$child_entities = $entity->{$k}
->referencedEntities();
foreach ($child_entities as $embedded_entity) {
if ($embedded_entity !== NULL) {
if ($embedded_entity instanceof ContentEntityInterface && $embedded_entity
->isTranslatable() && $this->lingotekConfiguration
->isEnabled($embedded_entity
->getEntityTypeId(), $embedded_entity
->bundle())) {
if (!isset($visited[$embedded_entity
->bundle()]) || !in_array($embedded_entity
->id(), $visited[$embedded_entity
->bundle()])) {
$entities = $this
->getNestedEntities($embedded_entity, $visited, $entities, $depth);
}
}
}
}
}
}
}
}
return $entities;
}
protected function getFilteredEntities() {
$visited = [];
$entities = [];
$recursion_depth = $this
->getRecursionDepth();
$entities = $this
->getNestedEntities($this->node, $visited, $entities, $recursion_depth);
return $entities;
}
protected function getRows($entity_list) {
$counter = 1;
$rows = [];
foreach ($entity_list as $entity_type_id => $entities) {
foreach ($entities as $entity_id => $entity) {
$rowId = (string) $entity
->getEntityTypeId() . ':' . (string) $entity
->id();
$rows[$rowId] = $this
->getRow($entity);
$counter += 1;
}
}
return $rows;
}
protected function getRow($entity) {
$row = parent::getRow($entity);
$bundleInfo = $this->entityTypeBundleInfo
->getBundleInfo($entity
->getEntityTypeId());
if ($entity
->hasLinkTemplate('canonical')) {
$row['label'] = $entity
->toLink();
}
else {
$row['label'] = $entity
->label();
}
$row['entity_type_id'] = $entity
->getEntityType()
->getLabel();
$row['bundle'] = $bundleInfo[$entity
->bundle()]['label'];
return $row;
}
protected function getTempStorageFilterKey() {
return NULL;
}
protected function getFilterKeys() {
return NULL;
}
protected function getFilters() {
return NULL;
}
protected function getPager() {
return NULL;
}
protected function getRecursionDepth() {
$temp_store = $this->tempStoreFactory
->get('lingotek.management.recursion_depth');
$depth = $temp_store
->get('depth');
if ($depth === NULL) {
$depth = 1;
}
return $depth;
}
protected function setRecursionDepth($depth) {
$temp_store = $this->tempStoreFactory
->get('lingotek.management.recursion_depth');
$temp_store
->set('depth', $depth);
}
public function recursionDepthCallback(array &$form, FormStateInterface $form_state) {
$value = $form_state
->getValue('depth');
$this
->setRecursionDepth($value);
}
protected function canHaveDeleteTranslationBulkOptions() {
return FALSE;
}
protected function canHaveDeleteBulkOptions() {
return FALSE;
}
}