TransactionType.php in Transaction 8
File
src/Entity/TransactionType.php
View source
<?php
namespace Drupal\transaction\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\transaction\TransactionTypeInterface;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
use Drupal\Core\Entity\EntityStorageInterface;
class TransactionType extends ConfigEntityBundleBase implements TransactionTypeInterface {
protected $id;
protected $label;
protected $target_entity_type;
protected $transactor;
protected $bundles = [];
protected $options = [];
protected $pluginCollection;
public function getTargetEntityTypeId() {
return $this->target_entity_type;
}
public function setTargetEntityTypeId($entity_type_id) {
$this->target_entity_type = $entity_type_id;
return $this;
}
public function getBundles($applicable = FALSE) {
$bundles = $this->bundles;
if (empty($bundles) && $applicable) {
$bundle_info_service = \Drupal::service('entity_type.bundle.info');
$bundle_info = $bundle_info_service
->getBundleInfo($this->target_entity_type);
$bundles = array_keys($bundle_info);
}
return $bundles;
}
protected function getPluginCollection() {
if (!$this->pluginCollection) {
$this->pluginCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.transaction.transactor'), $this->transactor['id'], $this->transactor['settings']);
}
return $this->pluginCollection;
}
public function getPluginId() {
return isset($this->transactor['id']) ? $this->transactor['id'] : NULL;
}
public function setPluginId($plugin_id) {
$this->transactor['id'] = $plugin_id;
$this->transactor['settings'] = [];
return $this;
}
public function getPluginSettings() {
return isset($this->transactor['settings']) ? $this->transactor['settings'] : [];
}
public function setPluginSettings(array $settings) {
$this->transactor['settings'] = $settings;
return $this;
}
public function getPlugin() {
$plugin = NULL;
if ($plugin_id = $this
->getPluginId()) {
$plugin = $this
->getPluginCollection()
->get($plugin_id);
}
return $plugin;
}
public function getOption($name, $default_value = NULL) {
return isset($this->options[$name]) ? $this->options[$name] : $default_value;
}
public function getOptions() {
return $this->options;
}
public function setOption($name, $value) {
$this->options[$name] = $value;
return $this;
}
public function setOptions(array $options) {
$this->options = $options;
return $this;
}
public function isApplicable(ContentEntityInterface $entity) {
if ($result = in_array($entity
->bundle(), $this
->getBundles(TRUE))) {
$result = $this
->getPlugin()
->isApplicable($entity, $this);
}
return $result;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$bundles = array_filter($this
->get('bundles'));
sort($bundles);
$this
->set('bundles', $bundles);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if ($update || \Drupal::isConfigSyncing()) {
return;
}
$entity_display_id = 'transaction.' . $this
->id() . '.list';
$entity_view_display_storage = $this
->entityTypeManager()
->getStorage('entity_view_display');
if ($entity_view_display_storage
->load($entity_display_id)) {
return;
}
$list_display_mode = $entity_view_display_storage
->create([
'id' => $entity_display_id,
'targetEntityType' => 'transaction',
'bundle' => $this
->id(),
'mode' => 'list',
'status' => TRUE,
]);
foreach ($list_display_mode
->getComponents() as $name => $component) {
if (isset($component['label']) && $component['label'] != 'hidden') {
$component['label'] = 'hidden';
$list_display_mode
->setComponent($name, $component);
}
}
$list_display_mode
->save();
}
public function calculateDependencies() {
$this
->addDependency('module', $this
->entityTypeManager()
->getDefinition($this->target_entity_type)
->getProvider());
$this
->calculatePluginDependencies($this
->getPlugin());
return $this;
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'add-form') {
$uri_route_parameters['target_entity_type'] = $this
->getTargetEntityTypeId();
$uri_route_parameters['transactor'] = $this
->getPluginId();
}
return $uri_route_parameters;
}
public function set($property_name, $value) {
if ($property_name === 'bundles') {
$clean_value = [];
if (is_array($value)) {
foreach ($value as $item) {
if (!empty($item) && is_string($item)) {
$clean_value[] = $item;
}
}
}
$value = $clean_value;
}
return parent::set($property_name, $value);
}
}