AssetInjectorBase.php in Asset Injector 8.2
File
src/Entity/AssetInjectorBase.php
View source
<?php
namespace Drupal\asset_injector\Entity;
use Drupal\asset_injector\AssetInjectorInterface;
use Drupal\asset_injector\AssetFileStorage;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Condition\ConditionPluginCollection;
abstract class AssetInjectorBase extends ConfigEntityBase implements AssetInjectorInterface, EntityWithPluginCollectionInterface {
public $id;
public $label;
public $code;
public $conditions_require_all = TRUE;
protected $conditions = [];
protected $contexts = [];
protected $conditionsCollection;
protected $conditionPluginManager;
public function libraryNameSuffix() {
$extension = $this
->extension();
return "{$extension}/{$this->id}";
}
public abstract function libraryInfo();
public abstract function extension();
public function internalFileUri() {
$storage = new AssetFileStorage($this);
return $storage
->createFile();
}
protected function filePathRelativeToDrupalRoot() {
$path = parse_url(file_create_url($this
->internalFileUri()), PHP_URL_PATH);
$path = str_replace(base_path(), '/', $path);
return $path;
}
public function getCode() {
return $this->code;
}
public function delete() {
$storage = new AssetFileStorage($this);
$storage
->deleteFiles();
parent::delete();
}
public function preSave(EntityStorageInterface $storage) {
$original_id = $this
->getOriginalId();
if ($original_id) {
$original = $storage
->loadUnchanged($original_id);
if ($original instanceof AssetInjectorInterface) {
$asset_file_storage = new AssetFileStorage($original);
$asset_file_storage
->deleteFiles();
}
}
parent::preSave($storage);
}
public function getPluginCollections() {
return [
'conditions' => $this
->getConditionsCollection(),
];
}
public function getConditions() {
return $this
->getConditionsCollection()
->getConfiguration();
}
public function setConditionsConfig($instance_id, array $configuration) {
$conditions = $this
->getConditionsCollection();
if (!$conditions
->has($instance_id)) {
$configuration['id'] = $instance_id;
$conditions
->addInstanceId($instance_id, $configuration);
}
else {
$conditions
->setInstanceConfiguration($instance_id, $configuration);
}
return $this;
}
public function getConditionsCollection() {
if (!isset($this->conditionsCollection)) {
$this->conditionsCollection = new ConditionPluginCollection($this
->conditionPluginManager(), $this
->get('conditions'));
}
return $this->conditionsCollection;
}
public function setConditionsCollection(ConditionPluginCollection $conditions) {
$this->conditionsCollection = $conditions;
}
public function getConditionsInstance($instance_id) {
return $this
->getConditionsCollection()
->get($instance_id);
}
protected function conditionPluginManager() {
if (!isset($this->conditionPluginManager)) {
$this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
}
return $this->conditionPluginManager;
}
}