You are here

function PhotoGrid::validate in Views Photo Grid 8

Validates the view configuration. Fails if there is a non-image field, or there are more than one image fields that are not excluded from display.

Overrides StylePluginBase::validate

File

src/Plugin/views/style/PhotoGrid.php, line 92
Contains \Drupal\views_photo_grid\Plugin\views\style\PhotoGrid.

Class

PhotoGrid
Style plugin to render the photo grid.

Namespace

Drupal\views_photo_grid\Plugin\views\style

Code

function validate() {
  $errors = parent::validate();
  if ($this->view->storage
    ->isNew()) {

    // Skip validation when the view is being created.
    // (the default field is a title field, which would fail.)
    return $errors;
  }

  // Get a list of fields that have been added to the display.
  $fields = $this->displayHandler->handlers['field'];

  // Check if there is exactly one image field to display.
  $fields_valid = TRUE;
  $field_count = 0;
  foreach ($fields as $key => $field) {

    // Ignore fields excluded from display.
    if (!empty($field->options['exclude'])) {
      continue;
    }

    // Determine the field's type.
    $field_storage_definitions = \Drupal::entityManager()
      ->getFieldStorageDefinitions($field->definition['entity_type']);
    $field_type = $field_storage_definitions[$field->field]
      ->getType();
    if ($field_type != 'image') {

      // Cannot display non-image fields. That would break the image grid.
      $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;
}