activitypub-1.0.x-dev/src/Plugin/Block/FollowBlock.php
src/Plugin/Block/FollowBlock.php
<?php
namespace Drupal\activitypub\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block to display a Follow form.
*
* @Block(
* id = "activitypub_follow",
* admin_label = @Translation("ActivityPub Follow"),
* )
*/
class FollowBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* FollowBlock constructor
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match'),
$container->get('entity_type.manager'),
$container->get('form_builder')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$actor = $this->entityTypeManager->getStorage('activitypub_actor')->loadActorByEntityIdAndType($this->routeMatch->getRawParameter('user'), 'person');
/** @noinspection PhpMethodParametersCountMismatchInspection */
return ['form' => \Drupal::formBuilder()->getForm('Drupal\activitypub\Form\FollowForm', $actor)];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 0;
}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
if ($this->routeMatch->getRouteName() == 'entity.user.canonical') {
if ($this->entityTypeManager->getStorage('activitypub_actor')->loadActorByEntityIdAndType($this->routeMatch->getRawParameter('user'), 'person')) {
return AccessResult::allowed();
}
return AccessResult::forbidden('No actor found for this user');
}
return AccessResult::forbidden('This block can only be rendered on the user profile page.');
}
}
