You are here

abstract class DemoContent in Open Social 10.3.x

Same name in this branch
  1. 10.3.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  2. 10.3.x modules/custom/social_demo/src/Annotation/DemoContent.php \Drupal\social_demo\Annotation\DemoContent
Same name and namespace in other branches
  1. 8.9 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  2. 8 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  3. 8.2 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  4. 8.3 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  5. 8.4 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  6. 8.5 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  7. 8.6 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  8. 8.7 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  9. 8.8 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  10. 10.0.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  11. 10.1.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  12. 10.2.x modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent

Class DemoContent.

@package Drupal\social_demo

Hierarchy

Expanded class hierarchy of DemoContent

File

modules/custom/social_demo/src/DemoContent.php, line 18

Namespace

Drupal\social_demo
View 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

Namesort descending Modifiers Type Description Overrides
ContainerFactoryPluginInterface::create public static function Creates an instance of the plugin. 120
DemoContent::$content protected property Contains the created content.
DemoContent::$data protected property Contains data from a file.
DemoContent::$entityStorage protected property Contains the entity storage.
DemoContent::$parser protected property Parser.
DemoContent::$profile protected property Profile.
DemoContent::checkMentionOrLinkByUuid protected function Extract the mention from the content by [~Uuid].
DemoContent::count public function Returns quantity of created items. Overrides DemoContentInterface::count 1
DemoContent::fetchData protected function Gets the data from a file.
DemoContent::getEntry abstract protected function Makes an array with data of an entity. 8
DemoContent::getModule public function Returns the module name. Overrides DemoContentInterface::getModule
DemoContent::getProfile public function Returns the profile. Overrides DemoContentInterface::getProfile
DemoContent::getSource public function Returns the file name. Overrides DemoContentInterface::getSource
DemoContent::loadByUuid protected function Load entity by uuid.
DemoContent::prepareImage protected function Prepares data about an image.
DemoContent::removeContent public function Removes content. Overrides DemoContentInterface::removeContent
DemoContent::scrambleData public function Scramble it. Overrides DemoContentInterface::scrambleData 2
DemoContent::setEntityStorage public function Set entity storage. Overrides DemoContentInterface::setEntityStorage
DemoContent::setProfile public function Sets the used profile. Overrides DemoContentInterface::setProfile
DemoContentInterface::createContent public function Creates content. 8
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.