FlowListBuilder.php in CMS Content Sync 2.0.x
File
src/Controller/FlowListBuilder.php
View source
<?php
namespace Drupal\cms_content_sync\Controller;
use Drupal\cms_content_sync\Entity\Flow;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlowListBuilder extends ConfigEntityListBuilder {
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_type, $storage);
$this->configFactory = $config_factory;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('config.factory'));
}
public function buildHeader() {
$header['name'] = $this
->t('Synchronization');
$header['id'] = $this
->t('Machine name');
$header['status'] = $this
->t('Status');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['name'] = $entity
->label();
$row['id'] = $entity
->id();
$config = $this->configFactory
->get('cms_content_sync.flow.' . $entity
->id());
$overwritten = $config
->get('status') != $entity
->status();
$active = $overwritten ? !$entity
->status() : $entity
->status();
$status = $active ? $this
->t('Active') : $this
->t('Inactive');
if ($overwritten) {
$status .= ' <i>(' . $this
->t('Overwritten') . ')</i>';
}
if ($active) {
$entity_type_configs = $entity
->getEntityTypeConfig(null, null, true);
$flow_config = \Drupal::config('cms_content_sync.flow.' . $entity
->id());
$version_mismatch = false;
foreach ($entity_type_configs as $config_key => $entity_type_config) {
$entity_type = $entity_type_config['entity_type_name'];
$bundle = $entity_type_config['bundle_name'];
$active_version = Flow::getEntityTypeVersion($entity_type, $bundle);
$config_version = $flow_config
->get('sync_entities.' . $config_key . '.version');
if ($active_version != $config_version) {
$version_mismatch = true;
break;
}
}
if ($version_mismatch) {
$status .= ' <i>(' . $this
->t('Version mismatch') . ')</i>';
}
}
$row['status'] = new FormattableMarkup($status, []);
return $row + parent::buildRow($entity);
}
public function render() {
$build = parent::render();
$status_overwritten = false;
$version_mismatch = false;
$rows = $build['table']['#rows'];
$settings = ContentSyncSettings::getInstance();
if (Migration::alwaysUseV2() && !$settings
->getSiteUuid()) {
$link = Link::fromTextAndUrl($this
->t('registered'), Url::fromRoute('cms_content_sync.site'))
->toString();
$this
->messenger()
->addWarning($this
->t('The site needs to be @link before creating a flow.', [
'@link' => $link,
]));
}
if (!empty($rows)) {
foreach ($rows as $row) {
if (strpos($row['status']
->__toString(), 'Overwritten')) {
$status_overwritten = true;
}
if (strpos($row['status']
->__toString(), 'Version mismatch')) {
$version_mismatch = true;
}
}
if ($status_overwritten) {
$build['explanation_overwritten'] = [
'#markup' => '<i>' . $this
->t('Overwritten: The status of this flow has been overwritten in a settings file.') . '</i><br>',
];
}
if ($version_mismatch) {
$build['explanation_version_mismatch'] = [
'#markup' => '<i>' . $this
->t('Version mismatch: The flow contains mismatching bundle versions and needs to be re-saved and exported.') . '</i>',
];
}
$build['hints'] = [
'#markup' => '<h3>' . $this
->t('Hints') . '</h3>' . $this
->t('You can enable / disable Flows using your settings.php file:'),
];
$overwrite_status = '<li>' . $this
->t('Status: <i>@overwrite_status</i>', [
'@overwrite_status' => '$config["cms_content_sync.flow.<flow_machine_name>"]["status"] = FALSE;',
]) . '</li>';
$build['hints_examples'] = [
'#markup' => '<ul>' . $overwrite_status . '</ul>',
];
}
return $build;
}
}