Commit a40ab426 authored by Neslee Canil Pinto's avatar Neslee Canil Pinto

Fixed Coding Standards in GalleryIdHelper.php

parent 026433f2
...@@ -9,15 +9,15 @@ use Drupal\Core\Field\FieldItemInterface; ...@@ -9,15 +9,15 @@ use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Utility\Token; use Drupal\Core\Utility\Token;
/** /**
* Class GalleryIdHelper * Class GalleryIdHelper.
* *
* @package Drupal\colorbox * @package Drupal\colorbox
*/ */
class GalleryIdHelper { class GalleryIdHelper {
/** /**
* @var null * @var null
*
*/ */
protected $galleryToken = NULL; protected $galleryToken = NULL;
...@@ -42,62 +42,61 @@ class GalleryIdHelper { ...@@ -42,62 +42,61 @@ class GalleryIdHelper {
$this->token = $token; $this->token = $token;
} }
/** /**
* @param \Drupal\Core\Entity\ContentEntityInterface $entity * @param \Drupal\Core\Entity\ContentEntityInterface $entity
* @param \Drupal\Core\Field\FieldItemInterface $item * @param \Drupal\Core\Field\FieldItemInterface $item
* @param array $settings * @param array $settings
* *
* @return string * @return string
*/ */
public function generateId(ContentEntityInterface $entity, FieldItemInterface $item, array $settings) {
public function generateId(ContentEntityInterface $entity, FieldItemInterface $item, array $settings) { $entity_bundle = $entity->bundle();
$entity_bundle = $entity->bundle(); $entity_type = $entity->getEntityTypeId();
$entity_type = $entity->getEntityTypeId(); $config = $this->configFactory->get('colorbox.settings');
$config = $this->configFactory->get('colorbox.settings');
// Build the gallery id.
// Build the gallery id. $id = $entity->id();
$id = $entity->id(); $entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id';
$entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id'; $field_name = $item->getParent()->getName();
$field_name = $item->getParent()->getName();
switch ($settings['colorbox_gallery']) {
switch ($settings['colorbox_gallery']) { case 'post':
case 'post': $gallery_id = 'gallery-' . $entity_id;
$gallery_id = 'gallery-' . $entity_id; break;
break;
case 'page':
case 'page': $gallery_id = 'gallery-all';
$gallery_id = 'gallery-all'; break;
break;
case 'field_post':
case 'field_post': $gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
$gallery_id = 'gallery-' . $entity_id . '-' . $field_name; break;
break;
case 'field_page':
case 'field_page': $gallery_id = 'gallery-' . $field_name;
$gallery_id = 'gallery-' . $field_name; break;
break;
case 'custom':
case 'custom': $gallery_id = $this->token->replace(
$gallery_id = $this->token->replace( $settings['colorbox_gallery_custom'], [$entity_type => $entity, 'file' => $item], ['clear' => TRUE]);
$settings['colorbox_gallery_custom'], [$entity_type => $entity,'file' => $item], ['clear' => TRUE]); break;
break;
default:
default: $gallery_id = '';
$gallery_id = ''; }
}
// If gallery id is not empty add unique per-request token to avoid.
// If gallery id is not empty add unique per-request token to avoid. // images being added manually to galleries.
// images being added manually to galleries. if (!empty($gallery_id) && $config->get('advanced.unique_token')) {
if (!empty($gallery_id) && $config->get('advanced.unique_token')) { // Check if gallery token has already been set, we need to reuse.
// Check if gallery token has already been set, we need to reuse. // the token for the whole request.
// the token for the whole request. if (is_null($this->galleryToken)) {
if (is_null($this->galleryToken)) { // We use a short token since randomness is not critical.
// We use a short token since randomness is not critical. $this->galleryToken = Crypt::randomBytesBase64(8);
$this->galleryToken = Crypt::randomBytesBase64(8); }
} $gallery_id = $gallery_id . '-' . $this->galleryToken;
$gallery_id = $gallery_id . '-' . $this->galleryToken;
}
return $gallery_id;
} }
return $gallery_id;
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment