Completed
Push — 8.x-1.x ( 657e32...c248cd )
by Janez
02:38
created

FileFieldFormatter::getEntityFromContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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