Problem/Motivation

View PDF link on views doesn't have any styling. Add option to add CSS classes to "View PDF" link on views.
button button-action button--primary button--small

Steps to reproduce

- add a View PDF link to views, there are no classes applied to the link and no options to apply styling.

Proposed resolution

Add Css classes option to View PDF link and render link with CSS classes.

Remaining tasks

- update class EntityPrintViewsLink
- add tests

User interface changes

- extra field added to View PDF link configuration

API changes

- extra config field added to View PDF link config yml

Data model changes

- extra config field added to View PDF link config yml

Here before:
before
And after:
view
Config:
after

CommentFileSizeAuthor
after_config.jpg44.4 KBady1503
after.jpg11.75 KBady1503
before.jpg10.37 KBady1503
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ady1503 created an issue. See original summary.

ady1503’s picture

Issue summary: View changes
ady1503’s picture

Issue summary: View changes
ady1503’s picture

I have little time now, the next option is to also transform the "View PDF" link into a button in the nodes.

It would be necessary to add this option in the module configuration form to be able to alter the link label per button with a CSS format.

When I have it I'm going to share it in case it works for someone.

If anyone wants to help, I say thank you in advance.

Thank you.

jannakha made their first commit to this issue’s fork.

jannakha’s picture

Title: Posibility to transform link View PDF to button. » Add css classes option to Print View link (on views).
Issue summary: View changes
Status: Active » Needs review

@ady1503 thanks for code you've provided. I've moved code to MR and updated task description.

Problem/Motivation

Hello.

I make some changes in code for make posibility to transform link "View PDF" in Views, from module to button.

Its must nicer view and for mi best option.

If you want add this is your module and your decision.

Here before:

alt

And after:

alt

And view:

alt

I add sinitize function for security reazon.

And for default button classes is empty, user need add some css for button, in mi example I use Olivero default button css:

button button-action button--primary button--small

Thanks for your module.

Gracias

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

<?php

namespace Drupal\entity_print_views\Plugin\views\area;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\entity_print\Plugin\ExportTypeManagerInterface;
use Drupal\views\Plugin\views\area\AreaPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Views area handler for a Print button.
 *
 * @ingroup views_area_handlers
 *
 * @ViewsArea("entity_print_views_link")
 */
class EntityPrintViewsLink extends AreaPluginBase {

  /**
   * The export type manager.
   *
   * @var \Drupal\entity_print\Plugin\ExportTypeManagerInterface
   */
  protected $exportTypeManager;

  /**
   * Constructs a new Entity instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\entity_print\Plugin\ExportTypeManagerInterface $export_type_manager
   *   The export type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ExportTypeManagerInterface $export_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->exportTypeManager = $export_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('plugin.manager.entity_print.export_type')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state): void {
    parent::buildOptionsForm($form, $form_state);
    $form['export_type'] = [
      '#type' => 'select',
      '#title' => $this->t('Export Type'),
      '#options' => $this->exportTypeManager->getFormOptions(),
      '#required' => TRUE,
      '#default_value' => $this->options['export_type'],
    ];
    $form['link_text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Link text'),
      '#required' => TRUE,
      '#default_value' => $this->options['link_text'],
    ];
    $form['class'] = [
      '#title' => $this->t('Class'),
      '#description' => $this->t('A CSS class to apply to the link. If using multiple classes, separate them by spaces.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['class'],
    ];

    $displays = $this->view->displayHandlers->getConfiguration();
    $display_options = [];
    foreach ($displays as $display_id => $display_info) {
      $display_options[$display_id] = $display_info['display_title'];
    }
    $form['display_id'] = [
      '#type' => 'select',
      '#title' => $this->t('View Display'),
      '#options' => $display_options,
      '#required' => TRUE,
      '#default_value' => $this->options['display_id'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render($empty = FALSE): array {
    if ($empty && empty($this->options['empty'])) {
      return [];
    }

    // Check if the user add css for button.
    if (!empty($this->options['class'])) {
      // Definir las clases proporcionadas.
      $user_defined_classes = $this->options['class'];
      // Sanitizar cada clase usando Xss::filter().
      $sanitized_classes = explode(' ', $user_defined_classes);
      foreach ($sanitized_classes as $class) {
      $sanitized_classes[] = Xss::filter($class);
      }
    } else {
      $sanitized_classes = [];
    }

    $route_params = [
      'export_type' => !empty($this->options['export_type']) ? $this->options['export_type'] : 'pdf',
      'view_name' => $this->view->storage->id(),
      'display_id' => $this->options['display_id'],
    ];

    return [
      '#type' => 'link',
      '#title' => $this->options['link_text'],
      '#url' => Url::fromRoute('entity_print_views.view', $route_params, [
        'query' => $this->view->getExposedInput() + ['view_args' => $this->view->args],
        'attributes' => [
          'class' => $sanitized_classes,
        ],
      ]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions(): array {
    $options = parent::defineOptions();
    $options['export_type'] = ['default' => 'pdf'];
    $options['link_text'] = ['default' => 'View PDF'];
    $options['class'] = ['default' => ''];
    $options['display_id'] = ['default' => $this->view->current_display];
    return $options;
  }
}

API changes

Data model changes

jannakha’s picture

Title: Add css classes option to Print View link (on views). » Add CSS classes option to View PDF link configuation (on views).

jannakha’s picture

Issue summary: View changes
jannakha’s picture

@ady1503 thanks for your work!
Re comment #4: create a new issue for optional css classes View PDF link on a node (to keep different features separate for easy testing/merging).

jannakha’s picture

Title: Add CSS classes option to View PDF link configuation (on views). » Add CSS classes option to View PDF link configuration (on views).
vladimiraus’s picture

Status: Needs review » Reviewed & tested by the community

Minor phpcs was fixed.
Everything else looks good and patch applies.
Thank you for contributing. 🎂

  • jannakha committed 9d376be3 on 8.x-2.x
    Issue #3502381: Add CSS classes option to View PDF link configuration (...
jannakha’s picture

Status: Reviewed & tested by the community » Fixed

merged

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.