EmbedButton.php in Embed 8
File
src/Entity/EmbedButton.php
View source
<?php
namespace Drupal\embed\Entity;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\embed\EmbedButtonInterface;
class EmbedButton extends ConfigEntityBase implements EmbedButtonInterface {
use StringTranslationTrait;
public $id;
public $label;
public $type_id;
public $type_settings = [];
public $icon = [];
public function getTypeId() {
return $this->type_id;
}
public function getTypeLabel() {
if ($definition = $this
->embedTypeManager()
->getDefinition($this
->getTypeId(), FALSE)) {
return $definition['label'];
}
return $this
->t('Unknown');
}
public function getTypePlugin() {
if ($plugin_id = $this
->getTypeId()) {
return $this
->embedTypeManager()
->createInstance($plugin_id, $this
->getTypeSettings());
}
}
public function getIconFile() {
@trigger_error(__METHOD__ . ' is deprecated in embed:8.x-1.2 and will be removed in embed:2.0.0. Use \\Drupal\\embed\\Entity\\EmbedButton::getIconUrl instead. See https://www.drupal.org/node/3139211', E_USER_DEPRECATED);
if (!empty($this->icon_uuid)) {
$files = $this
->entityTypeManager()
->getStorage('file')
->loadByProperties([
'uuid' => $this->icon_uuid,
]);
return reset($files);
}
}
public function getIconUrl() {
if (!empty($this->icon)) {
$uri = $this->icon['uri'];
if (!is_file($uri) && !UrlHelper::isExternal($uri)) {
static::convertEncodedDataToImage($this->icon);
}
$uri = file_create_url($uri);
}
else {
$uri = $this
->getTypePlugin()
->getDefaultIconUrl();
}
return file_url_transform_relative($uri);
}
public function calculateDependencies() {
parent::calculateDependencies();
if ($plugin = $this
->getTypePlugin()) {
$this
->calculatePluginDependencies($plugin);
return $this->dependencies;
}
return NULL;
}
protected function embedTypeManager() {
return \Drupal::service('plugin.manager.embed.type');
}
public function getTypeSetting($key, $default = NULL) {
if (isset($this->type_settings[$key])) {
return $this->type_settings[$key];
}
return $default;
}
public function getTypeSettings() {
return $this->type_settings;
}
public static function convertImageToEncodedData($uri) {
return [
'data' => base64_encode(file_get_contents($uri)),
'uri' => $uri,
];
}
public static function convertEncodedDataToImage(array $data) {
if (!is_file($data['uri'])) {
$directory = dirname($data['uri']);
$fileSystem = \Drupal::service('file_system');
$fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$fileSystem
->saveData(base64_decode($data['data']), $data['uri'], FileSystemInterface::EXISTS_REPLACE);
}
return $data['uri'];
}
}