class PushEntities in CMS Content Sync 2.1.x
Same name and namespace in other branches
- 8 src/Controller/PushEntities.php \Drupal\cms_content_sync\Controller\PushEntities
- 2.0.x src/Controller/PushEntities.php \Drupal\cms_content_sync\Controller\PushEntities
Pull controller.
Note that this controller is also used by the draggableviews submodule.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\cms_content_sync\Controller\PushEntities
Expanded class hierarchy of PushEntities
1 file declares its use of PushEntities
- cms_content_sync_draggableviews.module in modules/
cms_content_sync_draggableviews/ cms_content_sync_draggableviews.module
File
- src/
Controller/ PushEntities.php, line 17
Namespace
Drupal\cms_content_sync\ControllerView source
class PushEntities extends ControllerBase {
/**
* @var null|array
*/
protected $operations = [];
/**
* @var \Drupal\Core\StringTranslation\TranslatableMarkup
*/
protected $title;
/**
* @var array|string
*/
protected $callback;
/**
* @var bool
*/
protected $_showSkipped = false;
/**
* @var bool
*/
protected $_skipUnpushed = false;
/**
* @var array
*/
protected $_skippedUnpushed = [];
/**
* @var array
*/
protected $_skippedNoFlow = [];
/**
* PushEntities constructor.
*
* @param null|array $existing
*/
public function __construct($existing = null) {
$this->operations = $existing;
$this->title = t('Push content');
$this->callback = '\\Drupal\\cms_content_sync\\Controller\\PushEntities::batchFinished';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $existing = null) {
return new PushEntities($existing);
}
/**
* @return $this
*/
public function skipUnpushed() {
$this->_skipUnpushed = true;
return $this;
}
/**
* @return $this
*/
public function showSkipped() {
if ($count = count($this->_skippedNoFlow)) {
$list = [
'#theme' => 'item_list',
'#items' => $this
->getSkippedNoFlow(true),
];
\Drupal::messenger()
->addWarning(\Drupal::translation()
->translate("%count items were not pushed as they're not configured to be pushed: @items", [
'%count' => $count,
'@items' => \Drupal::service('renderer')
->render($list),
]));
}
if ($count = count($this->_skippedUnpushed)) {
$list = [
'#theme' => 'item_list',
'#items' => $this
->getSkippedUnpushed(true),
];
\Drupal::messenger()
->addStatus(\Drupal::translation()
->translate("%count items were not pushed as they weren't pushed before: @items", [
'%count' => $count,
'@items' => \Drupal::service('renderer')
->render($list),
]));
}
return $this;
}
/**
* @param bool $labelsOnly
*
* @return array
*/
public function getSkippedUnpushed($labelsOnly = false) {
return $labelsOnly ? $this
->getLabels($this->_skippedUnpushed) : $this->_skippedUnpushed;
}
/**
* @param bool $labelsOnly
*
* @return array
*/
public function getSkippedNoFlow($labelsOnly = false) {
return $labelsOnly ? $this
->getLabels($this->_skippedNoFlow) : $this->_skippedNoFlow;
}
/**
* @param \Drupal\Core\Entity\EntityInterface $entity
*
* @return $this
*/
public function addEntity($entity) {
$flows = PushIntent::getFlowsForEntity($entity, PushIntent::PUSH_FORCED, SyncIntent::ACTION_CREATE);
if (!count($flows)) {
$this->_skippedNoFlow[] = $entity;
return $this;
}
$flow_id = $flows[0]
->id();
/**
* @var \Drupal\cms_content_sync\Entity\EntityStatus[] $entity_status
*/
$entity_status = EntityStatus::getInfosForEntity($entity
->getEntityTypeId(), $entity
->uuid(), [
'flow' => $flow_id,
]);
if ($this->_skipUnpushed) {
if (!count($entity_status) || !$entity_status[0]
->getLastPush()) {
$this->_skippedUnpushed[] = $entity;
return $this;
}
}
$this
->add($flow_id, $entity
->getEntityTypeId(), $entity
->id());
return $this;
}
/**
* Get the operations that were already added. You can use them when instantiating this class again later to keep the
* existing operations and add on top of them.
*
* @return null|array
*/
public function get() {
return $this->operations;
}
/**
* @param string $flow_id
* @param string $entity_type_id
* @param int $entity_id
*
* @return $this
*/
public function add($flow_id, $entity_type_id, $entity_id) {
$this->operations[] = [
'\\Drupal\\cms_content_sync\\Controller\\PushEntities::batch',
[
$flow_id,
$entity_type_id,
$entity_id,
],
];
return $this;
}
/**
* @param string $set
*
* @return $this
*/
public function setTitle($set) {
$this->title = $set;
return $this;
}
/**
* @param array|string $set
*
* @return $this
*/
public function setCallback($set) {
$this->callback = $set;
return $this;
}
/**
* Start the actual batch operation.
*
* @param null|\Drupal\Core\Url $url
*
* @return null|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function start($url = null) {
$batch = [
'title' => $this->title,
'operations' => $this->operations,
'finished' => $this->callback,
];
batch_set($batch);
if ($url) {
return batch_process($url);
}
return null;
}
/**
* Check if there actually are any operations to perform now.
*
* @return bool
*/
public function isEmpty() {
return !count($this->operations);
}
/**
* Batch push finished callback.
*
* @param $success
* @param $results
* @param $operations
*/
public static function batchFinished($success, $results, $operations) {
$succeeded = count(array_filter($results));
\Drupal::messenger()
->addMessage(t('%synchronized items have been pushed to your @repository.', [
'@repository' => _cms_content_sync_get_repository_name(),
'%synchronized' => $succeeded,
]));
$failed = count($results) - $succeeded;
if ($failed) {
\Drupal::messenger()
->addMessage(t('%synchronized items have not been pushed to your @repository.', [
'@repository' => _cms_content_sync_get_repository_name(),
'%synchronized' => $failed,
]));
}
}
/**
* Batch push callback used by the following operations:
* - Flow: Push All
* - Content overview: Push changes
* - Draggableviews: Push changes.
*
* @param string $flow_id
* @param string $entity_type_id
* @param int $entity_id
* @param array $context
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function batch($flow_id, $entity_type_id, $entity_id, &$context) {
$message = 'Pushing...';
$results = [];
if (isset($context['results'])) {
$results = $context['results'];
}
/**
* @var \Drupal\cms_content_sync\Entity\Flow $flow
*/
$flow = Flow::getAll()[$flow_id];
/**
* @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
*/
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity = $entity_type_manager
->getStorage($entity_type_id)
->load($entity_id);
try {
$status = PushIntent::pushEntity($entity, PushIntent::PUSH_FORCED, SyncIntent::ACTION_CREATE, $flow);
} catch (\Exception $exception) {
\Drupal::messenger()
->addWarning(t('Item %label could not be pushed: %exception', [
'%label' => $entity
->label(),
'%exception' => $exception
->getMessage(),
]));
$status = false;
}
$results[] = $status;
$context['message'] = $message;
$context['results'] = $results;
}
/**
* @param $entities
*
* @return array
*/
protected function getLabels($entities) {
$result = [];
foreach ($entities as $entity) {
$result[] = $entity
->label();
}
return $result;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PushEntities:: |
protected | property | ||
PushEntities:: |
protected | property | ||
PushEntities:: |
protected | property | ||
PushEntities:: |
protected | property | ||
PushEntities:: |
protected | property | ||
PushEntities:: |
protected | property | ||
PushEntities:: |
protected | property | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public static | function | Batch push callback used by the following operations: | |
PushEntities:: |
public static | function | Batch push finished callback. | |
PushEntities:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
PushEntities:: |
public | function | Get the operations that were already added. You can use them when instantiating this class again later to keep the existing operations and add on top of them. | |
PushEntities:: |
protected | function | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | Check if there actually are any operations to perform now. | |
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | ||
PushEntities:: |
public | function | Start the actual batch operation. | |
PushEntities:: |
public | function | PushEntities constructor. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |