/**
  * Checks access to the 'unflag' action.
  *
  * @param \Drupal\flag\FlagInterface $flag
  *   The flag entity.
  *
  * @return string
  *   A \Drupal\Core\Access\AccessInterface constant value.
  */
 public function access(FlagInterface $flag) {
   return AccessResult::allowedIf($flag->hasActionAccess('unflag'));
 }
  /**
   * {@inheritdoc}
   */
  public function getLink(FlagInterface $flag, EntityInterface $entity) {
    $action = $flag->isFlagged($entity) ? 'unflag' : 'flag';

    if ($flag->hasActionAccess($action)) {

      $link = $this->buildLink($action, $flag, $entity);

      // The actual render array must be in a nested key, due to a bug in
      // lazy builder handling that does not properly render top-level #type
      // elements.
      return ['link' => $link];
    }

    return [];
  }