You are here

public function DemoNode::createFollow in Open Social 8

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

The function that checks and creates a follow on an entity.

Parameters

\Drupal\Core\Entity\Entity $entity: The related entity.

array $uuids: The array containing uuids.

1 call to DemoNode::createFollow()
DemoNode::createContent in modules/custom/social_demo/src/DemoNode.php
Creates content.

File

modules/custom/social_demo/src/DemoNode.php, line 199

Class

DemoNode
Class DemoNode.

Namespace

Drupal\social_demo

Code

public function createFollow(Entity $entity, array $uuids) {
  foreach ($uuids as $uuid) {

    // Load the user(s) by the given uuid(s).
    $account = $this
      ->loadByUuid('user', $uuid);
    $properties = [
      'uid' => $account
        ->id(),
      'entity_type' => 'node',
      'entity_id' => $entity
        ->id(),
      'flag_id' => 'follow_content',
    ];

    // Check the current flaggings.
    $flaggings = \Drupal::entityTypeManager()
      ->getStorage('flagging')
      ->loadByProperties($properties);
    $flagging = reset($flaggings);

    // If the user is already following, then nothing happens.
    // Else we create the flagging with the properties above.
    if (empty($flagging)) {
      $flagging = Flagging::create($properties);
      if ($flagging) {
        $flagging
          ->save();
      }
    }
  }
}