diff --git a/src/Plugin/views/field/FlagViewsLinkField.php b/src/Plugin/views/field/FlagViewsLinkField.php index 03a4685..55675ed 100644 --- a/src/Plugin/views/field/FlagViewsLinkField.php +++ b/src/Plugin/views/field/FlagViewsLinkField.php @@ -2,13 +2,13 @@ namespace Drupal\flag\Plugin\views\field; -use Drupal\Component\Serialization\Json; -use Drupal\Core\Link; -use Drupal\flag\Plugin\ActionLink\FormEntryInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\flag\FlagLinkBuilderInterface; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\ResultRow; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\EntityInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a views field to flag or unflag the selected content. @@ -18,7 +18,34 @@ use Drupal\Core\Entity\EntityInterface; * * @ViewsField("flag_link") */ -class FlagViewsLinkField extends FieldPluginBase { +class FlagViewsLinkField extends FieldPluginBase implements ContainerFactoryPluginInterface { + + /** + * The builder for flag links. + * + * @var \Drupal\flag\FlagLinkBuilderInterface + */ + protected $flagLinkBuilder; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('flag.link_builder') + ); + } + + /** + * {@inheritdoc} + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, FlagLinkBuilderInterface $flag_link_builder) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->flagLinkBuilder = $flag_link_builder; + } /** * A helper method to retrieve the flag entity from the views relationship. @@ -119,27 +146,9 @@ class FlagViewsLinkField extends FieldPluginBase { return ''; } - $flag = $this->getFlag(); - $link_type_plugin = $flag->getLinkTypePlugin(); - - $link = $link_type_plugin->getAsLink($flag, $entity); - - $renderable = $link->toRenderable(); - - if ($link_type_plugin instanceof FormEntryInterface) { - // Check if form should be in a modal or dialog. - $configuration = $link_type_plugin->getConfiguration(); - if ($configuration['form_behavior'] !== 'default') { - $renderable['#attached']['library'][] = 'core/drupal.ajax'; - $renderable['#attributes']['class'][] = 'use-ajax'; - $renderable['#attributes']['data-dialog-type'] = $configuration['form_behavior']; - $renderable['#attributes']['data-dialog-options'] = Json::encode([ - 'width' => 'auto', - ]); - } - } - - return $renderable; + return $this->flagLinkBuilder->build( + $entity->getEntityTypeId(), $entity->id(), $this->getFlag()->id() + ); } }