| 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 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |    * Constructs an AbstractHandler object. | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |    * @param object $entity | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |    *   The simulated entity object containing field information. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |    * @param string $entity_type | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |    *   The entity type. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |    * @param string $field_name | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |    *   The field name. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |    * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |    *   Thrown when the given field name does not exist on the entity. | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |   public function __construct(\stdClass $entity, $entity_type, $field_name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     if (empty($entity_type)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |       throw new \Exception("You must specify an entity type in order to parse entity fields."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     $entity_field_manager = \Drupal::service('entity_field.manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     $fields = $entity_field_manager->getFieldStorageDefinitions($entity_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     $this->fieldInfo = $fields[$field_name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     // The bundle may be stored either under "step_bundle" or under the name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     // of the entity's bundle key. If both are empty, assume this is a single | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     // bundle entity, and therefore make the bundle name the entity type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     $bundle_key = \Drupal::entityTypeManager()->getDefinition($entity_type)->getKey('bundle'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     $bundle = !empty($entity->$bundle_key) ? $entity->$bundle_key : (isset($entity->step_bundle) ? $entity->step_bundle : $entity_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     $fields = $entity_field_manager->getFieldDefinitions($entity_type, $bundle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     $fieldsstring = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     foreach ($fields as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |       $fieldsstring = $fieldsstring . ", " . $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     if (empty($fields[$field_name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |       throw new \Exception(sprintf('The field "%s" does not exist on entity type "%s" bundle "%s".', $field_name, $entity_type, $bundle)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     $this->fieldConfig = $fields[$field_name]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |   } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  |