FileFieldFormatter::getFieldValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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