Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / fontyourface / src / Entity / Font.php
1 <?php
2
3 namespace Drupal\fontyourface\Entity;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\Entity\ContentEntityBase;
7 use Drupal\Core\Entity\EntityChangedTrait;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\fontyourface\FontInterface;
10
11 /**
12  * Defines the Font entity.
13  *
14  * @ingroup fontyourface
15  *
16  * @ContentEntityType(
17  *   id = "font",
18  *   label = @Translation("Font"),
19  *   handlers = {
20  *     "storage_schema" = "Drupal\fontyourface\FontStorageSchema",
21  *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
22  *     "list_builder" = "Drupal\fontyourface\FontListBuilder",
23  *     "views_data" = "Drupal\fontyourface\Entity\FontViewsData",
24  *
25  *     "form" = {
26  *       "default" = "Drupal\fontyourface\Form\FontForm",
27  *       "edit" = "Drupal\fontyourface\Form\FontForm",
28  *     },
29  *     "access" = "Drupal\fontyourface\FontAccessControlHandler",
30  *     "route_provider" = {
31  *       "html" = "Drupal\fontyourface\FontHtmlRouteProvider",
32  *     },
33  *   },
34  *   base_table = "fontyourface_font",
35  *   admin_permission = "administer font entities",
36  *   entity_keys = {
37  *     "id" = "fid",
38  *     "label" = "name",
39  *     "uuid" = "uuid",
40  *     "pid" = "pid",
41  *     "url" = "url",
42  *   },
43  *   links = {
44  *     "canonical" = "/admin/appearance/font/{font}",
45  *     "enable" = "/admin/appearance/font/{font}/{js}/enable",
46  *     "disable" = "/admin/appearance/font/{font}/{js}/disable",
47  *     "collection" = "/admin/appearance/font",
48  *   },
49  *   field_ui_base_route = "font.settings",
50  *   render_cache = FALSE
51  * )
52  */
53 class Font extends ContentEntityBase implements FontInterface {
54   use EntityChangedTrait;
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getProvider() {
60     return $this->get('pid')->value;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function setProvider($provider) {
67     $this->set('pid', $provider);
68     return $this;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getMetadata() {
75     return unserialize($this->get('metadata')->value);
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function setMetadata($metadata) {
82     $this->set('metadata', serialize($metadata));
83     return $this;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function getCreatedTime() {
90     return $this->get('created')->value;
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function setCreatedTime($timestamp) {
97     $this->set('created', $timestamp);
98     return $this;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getChangedTime() {
105     return $this->get('changed')->value;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function setChangedTime($timestamp) {
112     $this->set('changed', $timestamp);
113     return $this;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function isActivated() {
120     $config = \Drupal::config('fontyourface.settings');
121     $enabled_fonts = $config->get('enabled_fonts');
122     return in_array($this->url->value, $enabled_fonts);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function isDeactivated() {
129     return !$this->isActivated();
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function activate() {
136     $config = \Drupal::configFactory()->getEditable('fontyourface.settings');
137     if (!$this->isActivated()) {
138       $enabled_fonts = $config->get('enabled_fonts');
139       $enabled_fonts[] = $this->url->value;
140       $config->set('enabled_fonts', $enabled_fonts)
141         ->save();
142     }
143     $this->status->value = TRUE;
144     $this->save();
145     return $this->isActivated();
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function deactivate() {
152     $config = \Drupal::configFactory()->getEditable('fontyourface.settings');
153     $enabled_fonts = $config->get('enabled_fonts');
154     $enabled_fonts = array_diff($enabled_fonts, [$this->url->value]);
155     $config->set('enabled_fonts', $enabled_fonts)
156       ->save();
157     $this->status->value = FALSE;
158     $this->save();
159     return $this->isDeactivated();
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public static function loadActivatedFonts() {
166     $config = \Drupal::config('fontyourface.settings');
167     $enabled_fonts = $config->get('enabled_fonts');
168     $fonts = [];
169     foreach ($enabled_fonts as $enabled_font_url) {
170       $font = self::loadByUrl($enabled_font_url);
171       if (!empty($font)) {
172         $fonts[$font->url->value] = $font;
173       }
174     }
175     return $fonts;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public static function loadByUrl($font_url) {
182     $fonts = \Drupal::entityManager()->getStorage('font')->loadByProperties(['url' => $font_url]);
183     return reset($fonts);
184   }
185
186   /**
187    * {@inheritdoc}
188    */
189   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
190     $fields['fid'] = BaseFieldDefinition::create('integer')
191       ->setLabel(t('ID'))
192       ->setDescription(t('The ID of the Font entity.'))
193       ->setReadOnly(TRUE);
194     $fields['uuid'] = BaseFieldDefinition::create('uuid')
195       ->setLabel(t('UUID'))
196       ->setDescription(t('The UUID of the Font entity.'))
197       ->setReadOnly(TRUE);
198     $fields['pid'] = BaseFieldDefinition::create('string')
199       ->setLabel(t('Provider ID'))
200       ->setDescription(t('The font provider ID.'))
201       ->setSettings([
202         'max_length' => 50,
203         'text_processing' => 0,
204       ])
205       ->setDefaultValue('')
206       ->setDisplayOptions('view', [
207         'label' => 'hidden',
208         'type' => 'string',
209         'weight' => -4,
210       ])
211       ->setDisplayOptions('form', [
212         'type' => 'string_textfield',
213         'weight' => -4,
214       ])
215       ->setDisplayConfigurable('form', TRUE)
216       ->setDisplayConfigurable('view', TRUE);
217
218     $fields['url'] = BaseFieldDefinition::create('string')
219       ->setLabel(t('Font URL'))
220       ->setDescription(t('A URL for the font.'))
221       ->setSettings([
222         'max_length' => 191,
223         'text_processing' => 0,
224       ])
225       ->setDefaultValue('')
226       ->setDisplayOptions('view', [
227         'label' => 'above',
228         'type' => 'string',
229         'weight' => -4,
230       ])
231       ->setDisplayOptions('form', [
232         'type' => 'string_textfield',
233         'weight' => -4,
234       ])
235       ->setDisplayConfigurable('form', TRUE)
236       ->setDisplayConfigurable('view', TRUE);
237
238     $fields['name'] = BaseFieldDefinition::create('string')
239       ->setLabel(t('Name'))
240       ->setDescription(t('The name of the Font entity.'))
241       ->setSettings([
242         'max_length' => 255,
243         'text_processing' => 0,
244       ])
245       ->setDefaultValue('')
246       ->setDisplayOptions('view', [
247         'label' => 'above',
248         'type' => 'string',
249         'weight' => -4,
250       ])
251       ->setDisplayOptions('form', [
252         'type' => 'string_textfield',
253         'weight' => -4,
254       ])
255       ->setDisplayConfigurable('form', TRUE)
256       ->setDisplayConfigurable('view', TRUE);
257
258     $fields['css_family'] = BaseFieldDefinition::create('string')
259       ->setLabel(t('CSS Family'))
260       ->setDescription(t('CSS family for the font.'))
261       ->setSettings([
262         'max_length' => 255,
263         'text_processing' => 0,
264       ])
265       ->setDefaultValue('')
266       ->setDisplayOptions('view', [
267         'label' => 'above',
268         'type' => 'string',
269         'weight' => -4,
270       ])
271       ->setDisplayOptions('form', [
272         'type' => 'string_textfield',
273         'weight' => -4,
274       ])
275       ->setDisplayConfigurable('form', TRUE)
276       ->setDisplayConfigurable('view', TRUE);
277
278     $fields['css_style'] = BaseFieldDefinition::create('string')
279       ->setLabel(t('CSS Style'))
280       ->setDescription(t('CSS style for the font.'))
281       ->setSettings([
282         'max_length' => 255,
283         'text_processing' => 0,
284       ])
285       ->setDefaultValue('')
286       ->setDisplayOptions('view', [
287         'label' => 'above',
288         'type' => 'string',
289         'weight' => -4,
290       ])
291       ->setDisplayOptions('form', [
292         'type' => 'string_textfield',
293         'weight' => -4,
294       ])
295       ->setDisplayConfigurable('form', TRUE)
296       ->setDisplayConfigurable('view', TRUE);
297
298     $fields['css_weight'] = BaseFieldDefinition::create('string')
299       ->setLabel(t('CSS Weight'))
300       ->setDescription(t('CSS weight for the font.'))
301       ->setSettings([
302         'max_length' => 255,
303         'text_processing' => 0,
304       ])
305       ->setDefaultValue('')
306       ->setDisplayOptions('view', [
307         'label' => 'above',
308         'type' => 'string',
309         'weight' => -4,
310       ])
311       ->setDisplayOptions('form', [
312         'type' => 'string_textfield',
313         'weight' => -4,
314       ])
315       ->setDisplayConfigurable('form', TRUE)
316       ->setDisplayConfigurable('view', TRUE);
317
318     $fields['foundry'] = BaseFieldDefinition::create('string')
319       ->setLabel(t('Foundry'))
320       ->setDescription(t('Foundry for the font.'))
321       ->setSettings([
322         'max_length' => 255,
323         'text_processing' => 0,
324       ])
325       ->setDefaultValue('')
326       ->setDisplayOptions('view', [
327         'label' => 'above',
328         'type' => 'string',
329         'weight' => -4,
330       ])
331       ->setDisplayOptions('form', [
332         'type' => 'string_textfield',
333         'weight' => -4,
334       ])
335       ->setDisplayConfigurable('form', TRUE)
336       ->setDisplayConfigurable('view', TRUE);
337
338     $fields['foundry_url'] = BaseFieldDefinition::create('string')
339       ->setLabel(t('Foundry URL'))
340       ->setDescription(t('Foundry URL.'))
341       ->setSettings([
342         'max_length' => 255,
343         'text_processing' => 0,
344       ])
345       ->setDefaultValue('')
346       ->setDisplayOptions('view', [
347         'label' => 'above',
348         'type' => 'string',
349         'weight' => -4,
350       ])
351       ->setDisplayOptions('form', [
352         'type' => 'string_textfield',
353         'weight' => -4,
354       ])
355       ->setDisplayConfigurable('form', TRUE)
356       ->setDisplayConfigurable('view', TRUE);
357
358     $fields['license'] = BaseFieldDefinition::create('string')
359       ->setLabel(t('License'))
360       ->setDescription(t('Font License.'))
361       ->setSettings([
362         'max_length' => 255,
363         'text_processing' => 0,
364       ])
365       ->setDefaultValue('')
366       ->setDisplayOptions('view', [
367         'label' => 'above',
368         'type' => 'string',
369         'weight' => -4,
370       ])
371       ->setDisplayOptions('form', [
372         'type' => 'string_textfield',
373         'weight' => -4,
374       ])
375       ->setDisplayConfigurable('form', TRUE)
376       ->setDisplayConfigurable('view', TRUE);
377
378     $fields['license_url'] = BaseFieldDefinition::create('string')
379       ->setLabel(t('License URL'))
380       ->setDescription(t('License URL.'))
381       ->setSettings([
382         'max_length' => 255,
383         'text_processing' => 0,
384       ])
385       ->setDefaultValue('')
386       ->setDisplayOptions('view', [
387         'label' => 'above',
388         'type' => 'string',
389         'weight' => -4,
390       ])
391       ->setDisplayOptions('form', [
392         'type' => 'string_textfield',
393         'weight' => -4,
394       ])
395       ->setDisplayConfigurable('form', TRUE)
396       ->setDisplayConfigurable('view', TRUE);
397
398     $fields['designer'] = BaseFieldDefinition::create('string')
399       ->setLabel(t('Designer'))
400       ->setDescription(t('Font Designer'))
401       ->setSettings([
402         'max_length' => 255,
403         'text_processing' => 0,
404       ])
405       ->setDefaultValue('')
406       ->setDisplayOptions('view', [
407         'label' => 'above',
408         'type' => 'string',
409         'weight' => -4,
410       ])
411       ->setDisplayOptions('form', [
412         'type' => 'string_textfield',
413         'weight' => -4,
414       ])
415       ->setDisplayConfigurable('form', TRUE)
416       ->setDisplayConfigurable('view', TRUE);
417
418     $fields['designer_url'] = BaseFieldDefinition::create('string')
419       ->setLabel(t('Designer URL'))
420       ->setDescription(t('Designer URL.'))
421       ->setSettings([
422         'max_length' => 255,
423         'text_processing' => 0,
424       ])
425       ->setDefaultValue('')
426       ->setDisplayOptions('view', [
427         'label' => 'above',
428         'type' => 'string',
429         'weight' => -4,
430       ])
431       ->setDisplayOptions('form', [
432         'type' => 'string_textfield',
433         'weight' => -4,
434       ])
435       ->setDisplayConfigurable('form', TRUE)
436       ->setDisplayConfigurable('view', TRUE);
437
438     $fields['metadata'] = BaseFieldDefinition::create('string_long')
439       ->setLabel(t('Font Metadata'))
440       ->setDescription(t('Additional Font Metadata'))
441       ->setDisplayConfigurable('form', TRUE)
442       ->setDisplayConfigurable('view', TRUE);
443
444     $fields['created'] = BaseFieldDefinition::create('created')
445       ->setLabel(t('Created'))
446       ->setDescription(t('The time that the entity was created.'));
447
448     $fields['changed'] = BaseFieldDefinition::create('changed')
449       ->setLabel(t('Changed'))
450       ->setDescription(t('The time that the entity was last edited.'));
451
452     $fields['status'] = BaseFieldDefinition::create('boolean')
453       ->setLabel(t('Font status'))
454       ->setDescription(t('A boolean indicating whether the font is enabled. Mostly used for views.'))
455       ->setTranslatable(TRUE)
456       ->setDefaultValue(FALSE);
457
458     return $fields;
459   }
460
461 }