View source
<?php
namespace Drupal\media_entity_pinterest\Plugin\MediaEntity\Type;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Pinterest extends MediaTypeBase {
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config_factory
->get('media_entity.settings'));
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('config.factory'));
}
public function defaultConfiguration() {
return [
'use_pinterest_api' => FALSE,
];
}
public static $validationRegexp = [
'@^\\s*(https?://)?(\\w+\\.)?pinterest\\.([a-zA-Z]+\\.)?([a-zA-Z]+)/pin/(?P<id>\\d+)/?\\s*$$@i' => 'id',
'@^\\s*(https?://)?(\\w+\\.)?pinterest\\.([a-zA-Z]+\\.)?([a-zA-Z]+)/(?P<username>\\w+)/(?P<slug>[\\w\\-_\\~]+)/?\\s*$@iu' => 'board',
'@^\\s*(https?://)?(\\w+\\.)?pinterest\\.([a-zA-Z]+\\.)?([a-zA-Z]+)/(?P<username>\\w+)/(?P<slug>[\\w\\-_\\~]+)/(?P<section>[\\w\\-_\\~%]+)/?\\s*$@iu' => 'section',
'@^\\s*(https?://)?(\\w+\\.)?pinterest\\.([a-zA-Z]+\\.)?([a-zA-Z]+)/(?P<username>\\w+)/?\\s*$@iu' => 'user',
];
public function providedFields() {
$fields = [
'id' => $this
->t('Pin ID'),
'board' => $this
->t('Board name'),
'section' => $this
->t('Section name'),
'user' => $this
->t('Pinterest user'),
];
return $fields;
}
public function getField(MediaInterface $media, $name) {
$matches = $this
->matchRegexp($media);
if (empty($matches)) {
return FALSE;
}
switch ($name) {
case 'id':
if (!empty($matches['id'])) {
return $matches['id'];
}
return FALSE;
case 'section':
if (!empty($matches['section'])) {
return $matches['section'];
}
return NULL;
case 'board':
if (!empty($matches['slug'])) {
return $matches['slug'];
}
return FALSE;
case 'user':
if (!empty($matches['username'])) {
return $matches['username'];
}
return FALSE;
}
return FALSE;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$bundle = $form_state
->getFormObject()
->getEntity();
$options = [];
$allowed_field_types = [
'string',
'string_long',
'link',
];
foreach ($this->entityFieldManager
->getFieldDefinitions('media', $bundle
->id()) as $field_name => $field) {
if (in_array($field
->getType(), $allowed_field_types) && !$field
->getFieldStorageDefinition()
->isBaseField()) {
$options[$field_name] = $field
->getLabel();
}
}
$form['source_field'] = [
'#type' => 'select',
'#title' => $this
->t('Field with source information'),
'#description' => $this
->t('Field on media entity that stores Pinterest embed code or URL. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
'#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
'#options' => $options,
];
$form['use_pinterest_api'] = [
'#type' => 'select',
'#title' => $this
->t('Whether to use Pinterest api to fetch pin or not.'),
'#description' => $this
->t("In order to use Pinterest's api you have to create a developer account and an application. For more information consult the readme file."),
'#default_value' => empty($this->configuration['use_pinterest_api']) ? 0 : $this->configuration['use_pinterest_api'],
'#options' => [
0 => $this
->t('No'),
],
];
return $form;
}
public function attachConstraints(MediaInterface $media) {
parent::attachConstraints($media);
if (isset($this->configuration['source_field'])) {
$source_field_name = $this->configuration['source_field'];
if ($media
->hasField($source_field_name)) {
foreach ($media
->get($source_field_name) as &$embed_code) {
$typed_data = $embed_code
->getDataDefinition();
$typed_data
->addConstraint('PinEmbedCode');
}
}
}
}
public function getDefaultThumbnail() {
return $this->config
->get('icon_base') . '/pinterest.png';
}
public function thumbnail(MediaInterface $media) {
if ($local_image = $this
->getField($media, 'thumbnail_local')) {
return $local_image;
}
return $this
->getDefaultThumbnail();
}
protected function matchRegexp(MediaInterface $media) {
$matches = [];
if (isset($this->configuration['source_field'])) {
$source_field = $this->configuration['source_field'];
if ($media
->hasField($source_field)) {
$property_name = $media->{$source_field}
->first()
->mainPropertyName();
foreach (static::$validationRegexp as $pattern => $key) {
if (preg_match($pattern, urldecode($media->{$source_field}->{$property_name}), $matches)) {
return $matches;
}
}
}
}
return FALSE;
}
public function getDefaultName(MediaInterface $media) {
$id = $this
->getField($media, 'id');
$board = $this
->getField($media, 'board');
$user = $this
->getField($media, 'user');
if (!empty($id)) {
return $id;
}
if (!empty($user) && !empty($board)) {
return $user . ' - ' . $board;
}
if (!empty($user) && empty($board)) {
return $user;
}
return parent::getDefaultName($media);
}
}