Completed
Push — 8.x-1.x ( c248cd...375743 )
by Janez
02:14
created

ViewModeFieldFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 44
rs 10
wmc 4
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldFormatter() 22 22 2
A buildConfigurationForm() 0 4 1
A getFieldFormatterId() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 entity reference field formatters.
9
 *
10
 * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
11
 *
12
 * @EntityEmbedDisplay(
13
 *   id = "view_mode",
14
 *   label = @Translation("View Mode"),
15
 *   deriver = "Drupal\entity_embed\Plugin\Derivative\ViewModeDeriver",
16
 *   field_type = "entity_reference"
17
 * )
18
 */
19
class ViewModeFieldFormatter extends EntityReferenceFieldFormatter {
20
21
  /**
22
   * {@inheritdoc}
23
   */
24 View Code Duplication
  public function getFieldFormatter() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    if (!isset($this->fieldFormatter)) {
26
      $display = [
27
        'type' => $this->getFieldFormatterId(),
28
        'settings' => [
29
          'view_mode' => $this->getPluginDefinition()['view_mode'],
30
        ],
31
        'label' => 'hidden',
32
      ];
33
34
      // Create the formatter plugin. Will use the default formatter for that
35
      // field type if none is passed.
36
      $this->fieldFormatter = $this->formatterPluginManager->getInstance(
37
        [
38
          'field_definition' => $this->getFieldDefinition(),
39
          'view_mode' => '_entity_embed',
40
          'configuration' => $display,
41
        ]
42
      );
43
    }
44
    return $this->fieldFormatter;
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
51
    // Configuration form is not needed as the view mode is defined implicitly.
52
    return [];
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function getFieldFormatterId() {
59
    return 'entity_reference_entity_view';
60
  }
61
62
}
63