You are here

PhotosImageSetCover.php in Album Photos 6.0.x

Same filename and directory in other branches
  1. 8.5 src/Plugin/views/field/PhotosImageSetCover.php

File

src/Plugin/views/field/PhotosImageSetCover.php
View source
<?php

namespace Drupal\photos\Plugin\views\field;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\LinkBase;
use Drupal\views\ResultRow;

/**
 * Field handler to present a link to set image as album cover.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("photos_image_set_cover")
 */
class PhotosImageSetCover extends LinkBase {

  /**
   * {@inheritdoc}
   */
  protected function renderLink(ResultRow $row) {
    if ($this->options['output_url_as_text']) {
      return $this
        ->getUrlInfo($row)
        ->toString();
    }
    $this->options['alter']['query'] = $this
      ->getDestinationArray();
    return parent::renderLink($row);
  }

  /**
   * {@inheritdoc}
   */
  protected function getUrlInfo(ResultRow $row) {
    $entity = $this
      ->getEntity($row);
    return Url::fromRoute('photos.album.update.cover', [
      'node' => $entity
        ->getAlbumId(),
      'photos_image' => $entity
        ->id(),
    ])
      ->setAbsolute($this->options['absolute']);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultLabel() {
    return $this
      ->t('Set to Cover');
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['output_url_as_text'] = [
      'default' => FALSE,
    ];
    $options['absolute'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['output_url_as_text'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Output the URL as text'),
      '#default_value' => $this->options['output_url_as_text'],
    ];
    $form['absolute'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use absolute link (begins with "http://")'),
      '#default_value' => $this->options['absolute'],
      '#description' => $this
        ->t('Enable this option to output an absolute link. Required if you want to use the path as a link destination.'),
    ];
    parent::buildOptionsForm($form, $form_state);

    // Only show the 'text' field if we don't want to output the raw URL.
    $form['text']['#states']['visible'][':input[name="options[output_url_as_text]"]'] = [
      'checked' => FALSE,
    ];
  }

}

Classes

Namesort descending Description
PhotosImageSetCover Field handler to present a link to set image as album cover.