abstract class DemoContent in Open Social 10.0.x
Same name in this branch
- 10.0.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 10.0.x modules/custom/social_demo/src/Annotation/DemoContent.php \Drupal\social_demo\Annotation\DemoContent
Same name and namespace in other branches
- 8.9 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.2 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.3 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.4 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.5 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.6 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.7 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 8.8 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 10.3.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 10.1.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
- 10.2.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
Class DemoContent.
@package Drupal\social_demo
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\social_demo\DemoContent implements DemoContentInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DemoContent
File
- modules/
custom/ social_demo/ src/ DemoContent.php, line 18
Namespace
Drupal\social_demoView source
abstract class DemoContent extends PluginBase implements DemoContentInterface {
/**
* Contains the created content.
*
* @var array
*/
protected $content = [];
/**
* Contains data from a file.
*
* @var array
*/
protected $data = [];
/**
* Parser.
*
* @var \Drupal\social_demo\DemoContentParserInterface
*/
protected $parser;
/**
* Profile.
*
* @var string
*/
protected $profile = '';
/**
* Contains the entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* {@inheritdoc}
*/
public function getSource() {
$definition = $this
->getPluginDefinition();
return isset($definition['source']) ? $definition['source'] : NULL;
}
/**
* {@inheritdoc}
*/
public function setProfile($profile) {
$this->profile = $profile;
}
/**
* {@inheritdoc}
*/
public function getModule() {
$definition = $this
->getPluginDefinition();
return isset($definition['provider']) ? $definition['provider'] : NULL;
}
/**
* {@inheritdoc}
*/
public function getProfile() {
return isset($this->profile) ? $this->profile : '';
}
/**
* {@inheritdoc}
*/
public function removeContent() {
$data = $this
->fetchData();
foreach ($data as $uuid => $item) {
// Must have uuid and same key value.
if ($uuid !== $item['uuid']) {
continue;
}
$entities = $this->entityStorage
->loadByProperties([
'uuid' => $uuid,
]);
foreach ($entities as $entity) {
$entity
->delete();
}
}
}
/**
* {@inheritdoc}
*/
public function count() {
return count($this->content);
}
/**
* {@inheritdoc}
*/
public function setEntityStorage(EntityStorageInterface $entity_storage) {
$this->entityStorage = $entity_storage;
}
/**
* Gets the data from a file.
*/
protected function fetchData() {
if (!$this->data) {
$this->data = $this->parser
->parseFileFromModule($this
->getSource(), $this
->getModule(), $this
->getProfile());
}
return $this->data;
}
/**
* Load entity by uuid.
*
* @param string $entity_type_id
* Identifier of entity type.
* @param string|int $id
* Identifier or uuid.
* @param bool $all
* If set true, method will return all loaded entity.
* If set false, will return only one.
*
* @return \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\EntityInterface[]|mixed
* Returns the entity.
*/
protected function loadByUuid($entity_type_id, $id, $all = FALSE) {
if (property_exists($this, $entity_type_id . 'Storage')) {
$storage = $this->{$entity_type_id . 'Storage'};
}
else {
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
}
if (is_numeric($id)) {
$entities = $storage
->loadByProperties([
'uid' => $id,
]);
}
else {
$entities = $storage
->loadByProperties([
'uuid' => $id,
]);
}
if (!$all) {
return current($entities);
}
return $entities;
}
/**
* Extract the mention from the content by [~Uuid].
*
* @param string $content
* The content that contains the mention.
*
* @return mixed
* If nothing needs to be replaced, just return the same content.
*/
protected function checkMentionOrLinkByUuid($content) {
// Check if there's a mention in the given content.
if (strpos($content, '[~') !== FALSE || strpos($content, '[link=') !== FALSE) {
// Put the content in a logical var.
$input = $content;
$mention_uuid = '';
$link_uuid = '';
// Uuid validation check.
$isValidUuid = '/^[0-9A-F]{8}-[0-9A-F]{4}-[1-5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i';
if (strpos($content, '[~') !== FALSE) {
// Strip the mention uuid from the content.
preg_match('/~(.*?)]/', $input, $output);
$mention_uuid = $output[1];
// If the uuid is not according the uuid v1 or v4 format
// then just return the content.
if (!preg_match($isValidUuid, $mention_uuid)) {
return $content;
}
}
if (strpos($content, '[link=') !== FALSE) {
// Strip the link uuid from the content.
preg_match('/=(.*?)]/', $input, $output);
$link_uuid = $output[1];
// If the uuid is not according the uuid v1 or v4 format
// then just return the content.
if (!preg_match($isValidUuid, $link_uuid)) {
return $content;
}
}
if (!empty($mention_uuid) || !empty($link_uuid)) {
// Load the account by uuid.
$account = $this
->loadByUuid('user', $mention_uuid);
if ($account instanceof User) {
// Load the profile by account id.
$profile = $this
->loadByUuid('profile', $account
->id());
if ($profile instanceof Profile) {
$mention = preg_replace('/' . $mention_uuid . '/', $profile
->id(), $content);
$content = $mention;
}
}
// Load the node by uuid.
$node = $this
->loadByUuid('node', $link_uuid);
if ($node instanceof Node) {
$options = [
'absolute' => TRUE,
];
$url = Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
], $options)
->toString();
// Prepare the link.
$link = '<a href="' . $url . '">' . $node
->getTitle() . '</a>';
// Replace the uuid with the link.
$link_replacement = preg_replace('/\\[link=' . $link_uuid . ']/', $link, $content);
$content = $link_replacement;
}
}
// Return the content with the replaced mention and/or link.
return $content;
}
// Return the content as it was given.
return $content;
}
/**
* Prepares data about an image.
*
* @param string $picture
* The image uuid.
* @param string $alt
* The image alt text.
*
* @return array
* Returns an array for the image field.
*/
protected function prepareImage($picture, $alt = '') {
$value = NULL;
$files = $this
->loadByUuid('file', $picture);
if ($files instanceof File) {
$value = [
[
'target_id' => $files
->id(),
'alt' => $alt ?: 'file' . $files
->id(),
],
];
}
return $value;
}
/**
* Makes an array with data of an entity.
*
* @param array $item
* Array with items.
*
* @return array
* Returns an array.
*/
protected abstract function getEntry(array $item);
/**
* Scramble it.
*
* @param array $data
* The data array to scramble.
* @param int|null $max
* How many items to generate.
*/
public function scrambleData(array $data, $max = NULL) {
$new_data = [];
for ($i = 0; $i < $max; $i++) {
// Get a random item from the array.
$old_uuid = array_rand($data);
$item = $data[$old_uuid];
$uuid = 'ScrambledDemo_' . time() . '_' . $i;
$item['uuid'] = $uuid;
$new_data[$uuid] = $item;
}
return $new_data;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContainerFactoryPluginInterface:: |
public static | function | Creates an instance of the plugin. | 120 |
DemoContent:: |
protected | property | Contains the created content. | |
DemoContent:: |
protected | property | Contains data from a file. | |
DemoContent:: |
protected | property | Contains the entity storage. | |
DemoContent:: |
protected | property | Parser. | |
DemoContent:: |
protected | property | Profile. | |
DemoContent:: |
protected | function | Extract the mention from the content by [~Uuid]. | |
DemoContent:: |
public | function |
Returns quantity of created items. Overrides DemoContentInterface:: |
1 |
DemoContent:: |
protected | function | Gets the data from a file. | |
DemoContent:: |
abstract protected | function | Makes an array with data of an entity. | 8 |
DemoContent:: |
public | function |
Returns the module name. Overrides DemoContentInterface:: |
|
DemoContent:: |
public | function |
Returns the profile. Overrides DemoContentInterface:: |
|
DemoContent:: |
public | function |
Returns the file name. Overrides DemoContentInterface:: |
|
DemoContent:: |
protected | function | Load entity by uuid. | |
DemoContent:: |
protected | function | Prepares data about an image. | |
DemoContent:: |
public | function |
Removes content. Overrides DemoContentInterface:: |
|
DemoContent:: |
public | function |
Scramble it. Overrides DemoContentInterface:: |
2 |
DemoContent:: |
public | function |
Set entity storage. Overrides DemoContentInterface:: |
|
DemoContent:: |
public | function |
Sets the used profile. Overrides DemoContentInterface:: |
|
DemoContentInterface:: |
public | function | Creates content. | 8 |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |