View source
<?php
namespace Drupal\cms_content_sync_views\Plugin\views\field;
use Drupal\cms_content_sync\Entity\EntityStatus;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class SyncState extends FieldPluginBase {
public function query() {
}
protected function defineOptions() {
$options = parent::defineOptions();
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
$entity = $values->_entity;
$messages = [];
$entity_status = EntityStatus::getInfosForEntity($entity
->getEntityTypeId(), $entity
->uuid());
foreach ($entity_status as $status) {
$entity_type_id = $entity
->getEntityTypeId();
$field_data_langcode = empty($values->{$entity_type_id . '_field_data_langcode'}) ? NULL : $values->{$entity_type_id . '_field_data_langcode'};
if (!is_null($field_data_langcode)) {
$entity = $entity
->getTranslation($field_data_langcode);
}
$pool = $status
->getPool();
if (!is_null($status
->getLastPull())) {
$source_url = $status
->getSourceUrl();
if (!is_null($source_url)) {
$url = Url::fromUri($source_url);
$link = Link::fromTextAndUrl(t('here'), $url);
$link = $link
->toRenderable();
$link['#attributes'] = [
'target' => '_blank',
];
$link_render = render($link);
$messages['pulled_from'] = $this
->t('Pulled from @link (@pool) at @date.', [
'@pool' => $pool ? $pool
->label() : $status
->get('pool')->value,
'@link' => $link_render,
'@date' => date('d.m.Y - H:i', $status
->getLastPull()),
]);
}
}
if ($entity
->getEntityTypeId() == 'redirect') {
$created = $entity
->getCreated();
}
else {
$created = $entity
->getCreatedTime();
}
if (!is_null($status
->getLastPush()) && $status
->getLastPush() >= $created) {
$show_usage_route_parameters = [
'entity' => $status
->getEntity()
->id(),
'entity_type' => $status
->getEntity()
->getEntityTypeId(),
];
$show_usage_url = Url::fromRoute('cms_content_sync.show_usage', $show_usage_route_parameters, [
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
]);
$show_usage = Link::fromTextAndUrl(t('Show usage'), $show_usage_url);
$show_usage = $show_usage
->toRenderable();
$messages['pushed_on'] = $this
->t('Pushed on @date to @pool - @show_usage.', [
'@date' => date('d.m.Y - H:i', $status
->getLastPush()),
'@pool' => $pool ? $pool
->label() : $status
->get('pool')->value,
'@show_usage' => render($show_usage),
]);
}
if ($entity
->getEntityTypeId() != 'redirect' && !is_null($status
->getLastPush()) && $status
->getLastPush() < $entity
->getChangedTime()) {
$push_changes_route_parameters = [
'flow_id' => $status
->getFlow()
->id(),
'entity' => $status
->getEntity()
->id(),
'entity_type' => $status
->getEntity()
->getEntityTypeId(),
];
$push_changes_url = Url::fromRoute('cms_content_sync.publish_changes', $push_changes_route_parameters, [
'query' => [
'destination' => Url::fromRoute('<current>')
->toString(),
],
]);
$push_changes = Link::fromTextAndUrl(t('Push changes'), $push_changes_url);
$push_changes = $push_changes
->toRenderable();
$messages['update_waiting'] = $this
->t('Update waiting to be pushed - @push_changes.', [
'@push_changes' => render($push_changes),
]);
}
if ($status
->isOverriddenLocally()) {
$messages['overridden_locally'] = $this
->t('Overridden locally.');
}
}
if (empty($messages)) {
$messages['not_syndicated'] = $this
->t('<em>Not pushed or pulled yet.</em>');
}
else {
$link = Link::createFromRoute('Sync status', 'cms_content_sync.content_sync_status', [
'node' => $entity
->id(),
])
->toString();
$messages['view_status'] = $link;
}
$renderable = [
'#theme' => 'sync_status',
'#messages' => $messages,
'#cache' => [
'max-age' => 0,
],
];
$rendered = \Drupal::service('renderer')
->render($renderable);
return $rendered;
}
}