|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_gallery\Plugin\Field\FieldFormatter; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Html; |
|
6
|
|
|
use Drupal\Component\Utility\UrlHelper; |
|
7
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
|
8
|
|
|
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter; |
|
9
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
10
|
|
|
use Drupal\Core\Render\Element; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Plugin implementation of the 'entity_reference_masonry' formatter. |
|
14
|
|
|
* |
|
15
|
|
|
* @FieldFormatter( |
|
16
|
|
|
* id = "entity_reference_masonry", |
|
17
|
|
|
* label = @Translation("Rendered as Masonry grid"), |
|
18
|
|
|
* field_types = { |
|
19
|
|
|
* "entity_reference" |
|
20
|
|
|
* } |
|
21
|
|
|
* ) |
|
22
|
|
|
*/ |
|
23
|
|
|
class EntityReferenceMasonryFormatter extends EntityReferenceEntityFormatter { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function defaultSettings() { |
|
29
|
|
|
return array( |
|
30
|
|
|
// Implement default settings. |
|
31
|
|
|
) + parent::defaultSettings(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function settingsForm(array $form, FormStateInterface $form_state) { |
|
38
|
|
|
return array( |
|
39
|
|
|
// Implement settings form. |
|
40
|
|
|
) + parent::settingsForm($form, $form_state); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function settingsSummary() { |
|
47
|
|
|
$summary = []; |
|
48
|
|
|
return $summary; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function viewElements(FieldItemListInterface $items, $langcode) { |
|
55
|
|
|
$elements = parent::viewElements($items, $langcode); |
|
56
|
|
|
$lightbox_id = Html::getUniqueId('df-tools-gallery'); |
|
57
|
|
|
foreach ($elements as &$element) { |
|
58
|
|
|
if (isset($element['#file']) && $element['#file']->bundle() === 'image') { |
|
59
|
|
|
$url = UrlHelper::filterBadProtocol((file_create_url($element['#file']->getFileUri()))); |
|
60
|
|
|
$element['#prefix'] = "<a href=\"$url\" data-lightbox=\"$lightbox_id\">"; |
|
61
|
|
|
$element['#suffix'] = '</a>'; |
|
62
|
|
|
} |
|
63
|
|
|
$element['#attributes']['class'][] = 'grid-item'; |
|
64
|
|
|
} |
|
65
|
|
|
$elements['#attached']['library'][] = 'df_tools_gallery/view'; |
|
66
|
|
|
$elements['#attributes']['class'][] = 'df-masonry-gallery'; |
|
67
|
|
|
return $elements; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|