View source
<?php
namespace Drupal\cms_content_sync\Plugin;
use Drupal\cms_content_sync\PullIntent;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync\SyncIntent;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use EdgeBox\SyncCore\Interfaces\Configuration\IDefineEntityType;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class FieldHandlerBase extends PluginBase implements ContainerFactoryPluginInterface, FieldHandlerInterface {
protected $logger;
protected $entityTypeName;
protected $bundleName;
protected $fieldName;
protected $fieldDefinition;
protected $settings;
protected $flow;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->logger = $logger;
$this->entityTypeName = $configuration['entity_type_name'];
$this->bundleName = $configuration['bundle_name'];
$this->fieldName = $configuration['field_name'];
$this->fieldDefinition = $configuration['field_definition'];
$this->settings = $configuration['settings'];
$this->flow = $configuration['sync'];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.factory')
->get('cms_content_sync'));
}
public function getFieldName() {
return $this->fieldName;
}
public function getAllowedPushOptions() {
return [
PushIntent::PUSH_DISABLED,
PushIntent::PUSH_AUTOMATICALLY,
];
}
public function getAllowedPullOptions() {
return [
PullIntent::PULL_DISABLED,
PullIntent::PULL_AUTOMATICALLY,
];
}
public function getHandlerSettings($current_values, $type = 'both') {
return [];
}
public function validateHandlerSettings(array &$form, FormStateInterface $form_state, $settings_key, $current_values) {
}
public function pull(PullIntent $intent) {
$action = $intent
->getAction();
$entity = $intent
->getEntity();
if (SyncIntent::ACTION_DELETE == $action) {
return false;
}
if ($intent
->shouldMergeChanges() && !$this
->forceMergeOverwrite()) {
return false;
}
if (PullIntent::PULL_AUTOMATICALLY != $this->settings['import']) {
return false;
}
if (!$entity
->isNew()) {
if ('default_langcode' === $this->fieldName) {
return true;
}
}
$data = $intent
->getProperty($this->fieldName);
if (empty($data)) {
$entity
->set($this->fieldName, null);
}
else {
$entity
->set($this->fieldName, $data);
}
return true;
}
public function push(PushIntent $intent) {
$action = $intent
->getAction();
$entity = $intent
->getEntity();
if (PushIntent::PUSH_AUTOMATICALLY != $this->settings['export']) {
return false;
}
if (SyncIntent::ACTION_DELETE == $action) {
return false;
}
$intent
->setProperty($this->fieldName, $entity
->get($this->fieldName)
->getValue());
return true;
}
public function definePropertyAtType(IDefineEntityType $type_definition) {
$type_definition
->addObjectProperty($this->fieldName, $this->fieldDefinition
->getLabel(), true, $this->fieldDefinition
->isRequired());
}
protected function forceMergeOverwrite() {
return 'changed' == $this->fieldName;
}
}