FileFieldFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildConfigurationForm() 0 14 1
A getFieldValue() 0 5 1
A defaultConfiguration() 0 6 1
1
<?php
2
3
namespace Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay;
4
5
use Drupal\Core\Form\FormStateInterface;
6
7
/**
8
 * Entity Embed Display reusing file field formatters.
9
 *
10
 * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
11
 *
12
 * @EntityEmbedDisplay(
13
 *   id = "file",
14
 *   label = @Translation("File"),
15
 *   entity_types = {"file"},
16
 *   deriver = "Drupal\entity_embed\Plugin\Derivative\FieldFormatterDeriver",
17
 *   field_type = "file",
18
 *   provider = "file"
19
 * )
20
 */
21
class FileFieldFormatter extends EntityReferenceFieldFormatter {
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  public function getFieldValue() {
27
    $value = parent::getFieldValue();
28
    $value += array_intersect_key($this->getConfiguration(), array('description' => ''));
29
    return $value;
30
  }
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public function defaultConfiguration() {
36
    $defaults = parent::defaultConfiguration();
37
    // Add support to store file description.
38
    $defaults['description'] = '';
39
    return $defaults;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
46
    $form = parent::buildConfigurationForm($form, $form_state);
47
48
    // Description is stored in the configuration since it doesn't map to an
49
    // actual HTML attribute.
50
    $form['description'] = array(
51
      '#type' => 'textfield',
52
      '#title' => $this->t('Description'),
53
      '#default_value' => $this->getConfigurationValue('description'),
54
      '#description' => $this->t('The description may be used as the label of the link to the file.'),
55
    );
56
57
    return $form;
58
  }
59
60
}
61