View source
<?php
namespace Drupal\sharemessage\Entity;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\node\NodeInterface;
use Drupal\sharemessage\ShareMessageInterface;
class ShareMessage extends ConfigEntityBase implements ShareMessageInterface {
public $id;
public $label;
public $enforce_usage;
public $settings;
public $title;
public $message_long;
public $message_short;
public $image_url;
public $image_width;
public $image_height;
public $fallback_image;
public $video_url;
public $share_url;
protected $plugin;
protected $extra_field_entity_type;
protected $extra_field_bundles = [];
protected $runtimeContext = [];
public function setLabel($label) {
$this
->set('label', $label);
return $this;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getPlugin() {
return \Drupal::service('plugin.manager.sharemessage.share')
->createInstance($this->plugin, [
'sharemessage' => $this,
]);
}
public function getPluginId() {
return $this->plugin;
}
public function setPluginID($plugin_id) {
$this->plugin = $plugin_id;
}
public function hasPlugin() {
if (!empty($this->plugin) && \Drupal::service('plugin.manager.sharemessage.share')
->hasDefinition($this->plugin)) {
return TRUE;
}
return FALSE;
}
public function getPluginDefinition() {
$share_plugin = $this
->getPlugin();
return $share_plugin
->getPluginDefinition();
}
public function getSetting($key) {
if (!empty($this->settings[$key])) {
return $this->settings[$key];
}
}
public function setRuntimeContext(array $context) {
$this->runtimeContext = $context;
}
public function getContext($view_mode = 'full') {
$context = [
'sharemessage' => $this,
'view_mode' => $view_mode,
];
$context += $this->runtimeContext;
$node = \Drupal::request()->attributes
->get('node');
if (!isset($context['node']) && $node instanceof NodeInterface) {
$context['node'] = $node;
}
\Drupal::moduleHandler()
->alter('sharemessage_token_context', $this, $context);
return $context;
}
public function buildOGTags($context) {
$tags = [];
$type = 'website';
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:title',
'content' => $this
->getTokenizedField($this->title, $context),
],
];
if ($image_url = $this
->getImageUrl($context, $fallback_image)) {
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image',
'content' => $image_url,
],
];
$image_width = NULL;
$image_height = NULL;
if ($fallback_image instanceof FileInterface) {
if (file_exists($fallback_image
->getFileUri())) {
$image = \Drupal::service('image.factory')
->get($fallback_image
->getFileUri());
if ($image
->isValid()) {
$image_width = $image
->getWidth();
$image_height = $image
->getHeight();
}
}
}
else {
$image_width = $this
->getTokenizedField($this->image_width, $context);
$image_height = $this
->getTokenizedField($this->image_height, $context);
}
if ($image_width && $image_height) {
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:width',
'content' => $image_width,
],
];
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:height',
'content' => $image_height,
],
];
}
}
if ($video_url = $this
->getTokenizedField($this->video_url, $context)) {
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:video',
'content' => $video_url . '?fs=1',
],
];
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:video:width',
'content' => \Drupal::config('sharemessage.addthis')
->get('shared_video_width'),
],
];
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:video:height',
'content' => \Drupal::config('sharemessage.addthis')
->get('shared_video_height'),
],
];
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:video:type',
'content' => 'application/x-shockwave-flash',
],
];
$type = 'video';
}
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:url',
'content' => $this
->getUrl($context),
],
];
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:description',
'content' => $this
->getTokenizedField($this->message_long, $context),
],
];
$tags[] = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:type',
'content' => $type,
],
];
return $tags;
}
public function buildTwitterCardTags($context) {
$twitter = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:card',
'content' => 'summary_large_image',
],
];
$tags[] = [
$twitter,
'twitter_card',
];
$twitter = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:site',
'content' => \Drupal::config('sharemessage.settings')
->get('twitter_user'),
],
];
$tags[] = [
$twitter,
'twitter_site',
];
$twitter = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:description',
'content' => $this
->getTokenizedField($this->message_long, $context),
],
];
$tags[] = [
$twitter,
'twitter_description',
];
if ($image_url = $this
->getImageUrl($context)) {
$twitter = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:image',
'content' => $image_url,
],
];
$tags[] = [
$twitter,
'twitter_image',
];
}
return $tags;
}
public function getTokenizedField($property_value, $context, $default = '') {
if ($property_value) {
return strip_tags(PlainTextOutput::renderFromHtml(\Drupal::token()
->replace($property_value, $context, [
'clear' => TRUE,
])));
}
return $default;
}
public function getUrl($context) {
$options = [
'absolute' => TRUE,
];
if ($this->enforce_usage) {
$options['query'] = [
'smid' => $this->id,
];
}
$uri = $this
->getTokenizedField($this->share_url, $context, Url::fromRoute('<current>')
->getInternalPath());
if (strpos($uri, '://') !== FALSE) {
return Url::fromUri($uri, $options)
->toString();
}
elseif ($url = \Drupal::pathValidator()
->getUrlIfValid($uri)) {
return $url
->setAbsolute()
->toString();
}
else {
return Url::fromUri('internal:/' . $uri, $options)
->toString();
}
}
public function getExtraFieldEntityType() {
return $this->extra_field_entity_type;
}
public function setExtraFieldEntityType($extra_field_entity_type) {
$this->extra_field_entity_type = $extra_field_entity_type;
}
public function getExtraFieldBundles() {
return $this->extra_field_bundles;
}
public function setExtraFieldBundles(array $extra_field_bundles) {
$this->extra_field_bundles = $extra_field_bundles;
}
protected function getImageUrl($context, &$fallback_image = NULL) {
$image_url = $this
->getTokenizedField($this->image_url, $context);
if (!$image_url && !empty($this->fallback_image)) {
$entity_repository = \Drupal::getContainer()
->get('entity.repository');
$fallback_image = $entity_repository
->loadEntityByUuid('file', $this->fallback_image);
if ($fallback_image) {
$image_url = file_create_url($fallback_image
->getFileUri());
}
}
return $image_url;
}
}