Completed
Pull Request — master (#101)
by
unknown
02:09
created

AbstractHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 6
Bugs 2 Features 1
Metric Value
wmc 10
c 6
b 2
f 1
lcom 0
cbo 0
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A slice_array_depth() 0 13 4
B __construct() 0 20 6
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
use Drupal\Driver\Fields\FieldHandlerInterface;
6
7
/**
8
 * Base class for field handlers in Drupal 8.
9
 */
10
abstract class AbstractHandler implements FieldHandlerInterface {
11
  /**
12
   * Field storage definition.
13
   *
14
   * @var \Drupal\field\Entity\FieldStorageConfig
15
   */
16
  protected $fieldInfo = NULL;
17
18
  /**
19
   * Field configuration definition.
20
   *
21
   * @var \Drupal\field\Entity\FieldConfig
22
   */
23
  protected $fieldConfig = NULL;
24
25
  protected function slice_array_depth($array, $depth = 0) {
26
    foreach ($array as $key => $value) {
27
        if (is_array($value)) {
28
            if ($depth > 0) {
29
                $array[$key] = slice_array_depth($value, $depth - 1);
30
            } else {
31
                unset($array[$key]);
32
            }
33
        }
34
    }
35
36
    return $array;
37
  }
38
  /**
39
   * Constructs an AbstractHandler object.
40
   *
41
   * @param \stdClass $entity
42
   *   The simulated entity object containing field information.
43
   * @param string $entity_type
44
   *   The entity type.
45
   * @param string $field_name
46
   *   The field name.
47
   *
48
   * @throws \Exception
49
   *   Thrown when the given field name does not exist on the entity.
50
   */
51
  public function __construct(\stdClass $entity, $entity_type, $field_name) {
52
	if (empty($entity_type)) {
53
	    throw new \Exception("You must specify an entity type in order to parse entity fields.");
54
	}
55
    $entity_manager = \Drupal::entityManager();
56
    $fields = $entity_manager->getFieldStorageDefinitions($entity_type);
57
    $this->fieldInfo = $fields[$field_name];
58
	
59
	// The bundle may be stored either under "step_bundle" or under the name of the entity's bundle key.
60
	// If both are empty, assume this is a single bundle entity, and therefore make the bundle name the entity type.
61
    $bundle_key = $entity_manager->getDefinition($entity_type)->getKey('bundle');
62
    $bundle = !empty($entity->$bundle_key) ? $entity->$bundle_key : (isset($entity->step_bundle) ? $entity->step_bundle : $entity_type);
63
64
    $fields = $entity_manager->getFieldDefinitions($entity_type, $bundle);
65
	$fieldsstring = '';foreach ($fields as $key => $value){$fieldsstring = $fieldsstring . ", " . $key;}
66
    if (empty($fields[$field_name])) {
67
      throw new \Exception(sprintf('The field "%s" does not exist on entity type "%s" bundle "%s".', $field_name, $entity_type, $bundle));
68
    }
69
    $this->fieldConfig = $fields[$field_name];
70
  }
71
72
}
73