DefaultConfigEntityHandler.php in CMS Content Sync 8
File
src/Plugin/cms_content_sync/entity_handler/DefaultConfigEntityHandler.php
View source
<?php
namespace Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler;
use Drupal\cms_content_sync\Plugin\EntityHandlerBase;
use Drupal\cms_content_sync\PullIntent;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync\SyncIntent;
use Drupal\Core\Entity\EntityInterface;
class DefaultConfigEntityHandler extends EntityHandlerBase {
public static function supports($entity_type, $bundle) {
$entity_types = [
'webform',
];
return in_array($entity_type, $entity_types);
}
public function updateEntityTypeDefinition(&$definition) {
$typeMapping = [
'uuid' => 'string',
'boolean' => 'boolean',
'email' => 'string',
'integer' => 'integer',
'float' => 'float',
'string' => 'string',
'text' => 'string',
'label' => 'string',
'uri' => 'string',
'mapping' => 'object',
'sequence' => 'object',
];
foreach ($this
->getConfigProperties() as $key => $config) {
$type = $config['type'];
if (empty($typeMapping[$type])) {
continue;
}
$remoteType = $typeMapping[$type];
$multiple = false;
if ('array' === $remoteType) {
$type = $config['sequence']['type'];
if (empty($typeMapping[$type])) {
continue;
}
$remoteType = $typeMapping[$type];
$multiple = true;
}
if ('string' === $remoteType) {
$definition
->addStringProperty($key, $multiple);
}
elseif ('boolean' === $remoteType) {
$definition
->addBooleanProperty($key, $multiple);
}
elseif ('integer' === $remoteType) {
$definition
->addIntegerProperty($key, $multiple);
}
elseif ('float' === $remoteType) {
$definition
->addFloatProperty($key, $multiple);
}
else {
$definition
->addObjectProperty($key, $multiple);
}
}
}
public function getAllowedPreviewOptions() {
return [];
}
public function push(PushIntent $intent, EntityInterface $entity = null) {
if (!parent::push($intent, $entity)) {
return false;
}
if (!$entity) {
$entity = $intent
->getEntity();
}
foreach ($this
->getConfigProperties() as $property => $config) {
$entity_property = $entity
->get($property);
if (is_array($entity_property) && !count($entity_property)) {
$entity_property = null;
}
$intent
->setProperty($property, $entity_property);
}
return true;
}
public function pull(PullIntent $intent) {
$action = $intent
->getAction();
if (!parent::pull($intent)) {
return false;
}
if (SyncIntent::ACTION_DELETE === $action) {
return true;
}
$entity = $intent
->getEntity();
$forbidden_fields = $this
->getForbiddenFields();
foreach ($this
->getConfigProperties() as $property => $config) {
if (in_array($property, $forbidden_fields)) {
continue;
}
$entity
->set($property, $intent
->getProperty($property));
}
$entity
->save();
return true;
}
protected function getConfigProperties() {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($this->entityTypeName);
$properties = $entity_type
->getPropertiesToExport();
if (!$properties) {
return [];
}
$config_definition = \Drupal::service('config.typed')
->getDefinition($this->entityTypeName . '.' . $this->bundleName . '.*');
if (empty($config_definition)) {
return [];
}
$mapping = $config_definition['mapping'];
$result = [];
foreach ($properties as $property) {
if ('webform' === $this->entityTypeName && 'access' === $property) {
$mapping[$property]['type'] = 'mapping';
}
$result[$property] = $mapping[$property];
}
return $result;
}
protected function hasLabelProperty() {
return true;
}
}