AcquiaCohesionDataSuggester.php in TMGMT Translator Smartling 8.4
File
modules/tmgmt_smartling_acquia_cohesion/src/AcquiaCohesionDataSuggester.php
View source
<?php
namespace Drupal\tmgmt_smartling_acquia_cohesion;
use Drupal\cohesion_elements\Entity\ComponentContent;
use Drupal\tmgmt\JobItemInterface;
use Psr\Log\LoggerInterface;
class AcquiaCohesionDataSuggester implements LayoutCanvasAwareInterface {
use LayoutCanvasAwareTrait;
protected $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function suggestCohesionContentComponents(array $jobItems) {
$suggestions = [];
foreach ($jobItems as $jobItem) {
$jobItemData = $jobItem
->getData();
$suggestions = array_merge($suggestions, $this
->walkRecursiveAndGetSuggestions($jobItemData, $jobItem));
}
return $suggestions;
}
protected function walkRecursiveAndGetSuggestions($data, JobItemInterface $jobItem) {
$suggestions = [];
if (is_string($data) && ($layoutCanvas = $this
->isLayoutCanvas($data))) {
foreach ($layoutCanvas
->iterateCanvas() as $element) {
if ($element
->isComponentContent()) {
$id = str_replace("cc_", "", $element
->getProperty("componentContentId"));
$componentContent = $this
->loadComponentContent($id);
if (empty($componentContent)) {
$this
->logger("Tried to suggest component content id=@id but failed to load it", [
"@id" => $id,
]);
continue;
}
$suggestions[] = [
'job_item' => tmgmt_job_item_create('content', $componentContent
->getEntityTypeId(), $componentContent
->id()),
'reason' => t('Referenced content component'),
'from_item' => $jobItem
->id(),
];
}
}
}
if (is_array($data)) {
foreach ($data as $key => $value) {
$suggestions += $this
->walkRecursiveAndGetSuggestions($value, $jobItem);
}
}
return $suggestions;
}
protected function loadComponentContent($id) {
return ComponentContent::load($id);
}
}