PhotoGrid.php in Views Photo Grid 8
File
src/Plugin/views/style/PhotoGrid.php
View source
<?php
namespace Drupal\views_photo_grid\Plugin\views\style;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Form\FormStateInterface;
class PhotoGrid extends StylePluginBase {
protected $usesFields = TRUE;
protected $usesRowPlugin = FALSE;
protected function defineOptions() {
$options = parent::defineOptions();
$options['grid_padding'] = array(
'default' => 1,
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['grid_padding'] = array(
'#type' => 'number',
'#title' => $this
->t('Padding'),
'#size' => 2,
'#description' => $this
->t('The amount of padding in pixels in between grid items.'),
'#default_value' => $this->options['grid_padding'],
'#maxlength' => 2,
);
}
public function get_image_field_name() {
$fields = $this->displayHandler->handlers['field'];
foreach ($fields as $key => $field) {
$field_storage_definitions = \Drupal::entityManager()
->getFieldStorageDefinitions($field->definition['entity_type']);
$field_type = $field_storage_definitions[$field->field]
->getType();
if (empty($field->options['exclude']) && $field_type == 'image') {
return $field->field;
}
}
return FALSE;
}
function validate() {
$errors = parent::validate();
if ($this->view->storage
->isNew()) {
return $errors;
}
$fields = $this->displayHandler->handlers['field'];
$fields_valid = TRUE;
$field_count = 0;
foreach ($fields as $key => $field) {
if (!empty($field->options['exclude'])) {
continue;
}
$field_storage_definitions = \Drupal::entityManager()
->getFieldStorageDefinitions($field->definition['entity_type']);
$field_type = $field_storage_definitions[$field->field]
->getType();
if ($field_type != 'image') {
$fields_valid = FALSE;
break;
}
$field_count++;
}
if (!$fields_valid || $field_count > 1) {
$errors[] = $this
->t('This format can display only one image field and no other fields.');
}
return $errors;
}
}
Classes
Name |
Description |
PhotoGrid |
Style plugin to render the photo grid. |