633cf4d9966c2926b07ca76735fded12c62f16d6
[yaffs-website] / web / modules / contrib / blazy / tests / modules / blazy_test / src / Form / BlazyAdminTest.php
1 <?php
2
3 namespace Drupal\blazy_test\Form;
4
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
6 use Drupal\Component\Utility\Html;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Drupal\blazy\Form\BlazyAdminInterface;
9 use Drupal\blazy\BlazyManagerInterface;
10
11 /**
12  * Provides resusable admin functions or form elements.
13  */
14 class BlazyAdminTest implements BlazyAdminTestInterface {
15
16   use StringTranslationTrait;
17
18   /**
19    * The blazy admin service.
20    *
21    * @var \Drupal\blazy\Form\BlazyAdminInterface
22    */
23   protected $blazyAdmin;
24
25   /**
26    * The blazy_test manager service.
27    *
28    * @var \Drupal\blazy_test\BlazyManagerInterface
29    */
30   protected $manager;
31
32   /**
33    * Static cache for the skin definition.
34    *
35    * @var array
36    */
37   protected $skinDefinition;
38
39   /**
40    * Static cache for the skin options.
41    *
42    * @var array
43    */
44   protected $skinOptions;
45
46   /**
47    * Constructs a GridStackAdmin object.
48    */
49   public function __construct(BlazyAdminInterface $blazy_admin, BlazyManagerInterface $manager) {
50     $this->blazyAdmin = $blazy_admin;
51     $this->manager = $manager;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function create(ContainerInterface $container) {
58     return new static($container->get('blazy.admin.extended'), $container->get('blazy.manager'));
59   }
60
61   /**
62    * Returns the blazy admin.
63    */
64   public function blazyAdmin() {
65     return $this->blazyAdmin;
66   }
67
68   /**
69    * Returns the blazy_test manager.
70    */
71   public function manager() {
72     return $this->manager;
73   }
74
75   /**
76    * Returns defined skins as registered via hook_blazy_test_skins_info().
77    */
78   public function getSkins() {
79     if (!isset($this->skinDefinition)) {
80       $this->skinDefinition = $this->manager->buildSkins('blazy_test', '\Drupal\blazy_test\BlazyTestSkin');
81     }
82
83     return $this->skinDefinition;
84   }
85
86   /**
87    * Returns all settings form elements.
88    */
89   public function buildSettingsForm(array &$form, $definition = []) {
90     $definition += [
91       'namespace'  => 'blazy',
92       'optionsets' => [],
93       'skins'      => $this->getSkinOptions(),
94       'grid_form'  => TRUE,
95       'style'      => TRUE,
96     ];
97
98     foreach (['background', 'caches', 'fieldable_form', 'id', 'vanilla'] as $key) {
99       $definition[$key] = TRUE;
100     }
101
102     $definition['layouts'] = isset($definition['layouts']) ? array_merge($this->getLayoutOptions(), $definition['layouts']) : $this->getLayoutOptions();
103
104     $this->openingForm($form, $definition);
105     $this->mainForm($form, $definition);
106     $this->closingForm($form, $definition);
107   }
108
109   /**
110    * Returns the opening form elements.
111    */
112   public function openingForm(array &$form, $definition = []) {
113     $this->blazyAdmin->openingForm($form, $definition);
114   }
115
116   /**
117    * Returns the main form elements.
118    */
119   public function mainForm(array &$form, $definition = []) {
120     if (!empty($definition['image_style_form'])) {
121       $this->blazyAdmin->imageStyleForm($form, $definition);
122     }
123
124     if (!empty($definition['media_switch_form'])) {
125       $this->blazyAdmin->mediaSwitchForm($form, $definition);
126     }
127
128     if (!empty($definition['grid_form'])) {
129       $this->blazyAdmin->gridForm($form, $definition);
130     }
131
132     if (!empty($definition['fieldable_form'])) {
133       $this->blazyAdmin->fieldableForm($form, $definition);
134     }
135
136     if (!empty($definition['breakpoints'])) {
137       $this->blazyAdmin->breakpointsForm($form, $definition);
138     }
139   }
140
141   /**
142    * Returns the closing form elements.
143    */
144   public function closingForm(array &$form, $definition = []) {
145     $this->blazyAdmin->closingForm($form, $definition);
146   }
147
148   /**
149    * Returns available blazy_test skins for select options.
150    */
151   public function getSkinOptions() {
152     if (!isset($this->skinOptions)) {
153       $this->skinOptions = [];
154       foreach ($this->getSkins() as $skin => $properties) {
155         $this->skinOptions[$skin] = Html::escape($properties['name']);
156       }
157     }
158
159     return $this->skinOptions;
160   }
161
162   /**
163    * Returns default layout options for the core Image, or Views.
164    */
165   public function getLayoutOptions() {
166     return [
167       'bottom' => $this->t('Caption bottom'),
168       'center' => $this->t('Caption center'),
169       'top'    => $this->t('Caption top'),
170     ];
171   }
172
173   /**
174    * Return the field formatter settings summary.
175    *
176    * @deprecated: Removed for self::getSettingsSummary().
177    */
178   public function settingsSummary($plugin, $definition = []) {
179     return $this->blazyAdmin->settingsSummary($plugin, $definition);
180   }
181
182   /**
183    * Return the field formatter settings summary.
184    *
185    * @todo: Remove second param $plugin for post-release for Blazy RC2+.
186    */
187   public function getSettingsSummary(array $definition = [], $plugin = NULL) {
188     // @todo: Remove condition for Blazy RC2+.
189     if (!method_exists($this->blazyAdmin, 'getSettingsSummary')) {
190       return $this->blazyAdmin->settingsSummary($plugin, $definition);
191     }
192
193     return $this->blazyAdmin->getSettingsSummary($definition);
194   }
195
196   /**
197    * Returns available fields for select options.
198    */
199   public function getFieldOptions($target_bundles = [], $allowed_field_types = [], $entity_type_id = 'media', $target_type = '') {
200     return $this->blazyAdmin->getFieldOptions($target_bundles, $allowed_field_types, $entity_type_id, $target_type);
201   }
202
203   /**
204    * Returns re-usable logic, styling and assets across fields and Views.
205    */
206   public function finalizeForm(array &$form, $definition = []) {
207     $this->blazyAdmin->finalizeForm($form, $definition);
208   }
209
210 }