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

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

  }
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 ( ) ; } } } }
The function that checks and creates a follow on an entity.

@param \Drupal\Core\Entity\Entity $entity
The related entity.
@param array $uuids
The array containing uuids.
The function that checks and creates a follow on an entity .