You are here

abstract class DemoContent in Open Social 8

Same name in this branch
  1. 8 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  2. 8 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.2 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  3. 8.3 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  4. 8.4 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  5. 8.5 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  6. 8.6 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  7. 8.7 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  8. 8.8 modules/custom/social_demo/src/DemoContent.php \Drupal\social_demo\DemoContent
  9. 10.3.x 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 17

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;
  }

  /**
   * 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);

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerFactoryPluginInterface::create public static function Creates an instance of the plugin. 112
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::removeContent public function Removes content. Overrides DemoContentInterface::removeContent
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 An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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 3
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. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.