You are here

protected function DemoContent::checkMentionOrLinkByUuid in Open Social 8

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

Extract the mention from the content by [~Uuid].

Parameters

string $content: The content that contains the mention.

Return value

mixed If nothing needs to be replaced, just return the same content.

3 calls to DemoContent::checkMentionOrLinkByUuid()
DemoComment::getEntry in modules/custom/social_demo/src/DemoComment.php
Makes an array with data of an entity.
DemoNode::getEntry in modules/custom/social_demo/src/DemoNode.php
Makes an array with data of an entity.
Post::getEntry in modules/custom/social_demo/src/Plugin/DemoContent/Post.php
Makes an array with data of an entity.

File

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

Class

DemoContent
Class DemoContent.

Namespace

Drupal\social_demo

Code

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