コード例 #1
0
 /**
  * Returns fields that should be embedded into the data for the given entity.
  *
  * Includes explicitly enabled fields and composite entities that are
  * implicitly included to the translatable data.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity to get the translatable data from.
  *
  * @return array $embeddable_fields
  *   Translatable data.
  */
 public function getEmbeddableFields(ContentEntityInterface $entity)
 {
     // Get the configurable embeddable references.
     $field_definitions = $entity->getFieldDefinitions();
     $embeddable_field_names = \Drupal::config('tmgmt_content.settings')->get('embedded_fields');
     $embeddable_fields = array_filter($field_definitions, function (FieldDefinitionInterface $field_definition) use($embeddable_field_names) {
         return !$field_definition->isTranslatable() && isset($embeddable_field_names[$field_definition->getTargetEntityTypeId()][$field_definition->getName()]);
     });
     // Get always embedded references.
     $content_translation_manager = \Drupal::service('content_translation.manager');
     foreach ($field_definitions as $field_name => $field_definition) {
         $storage_definition = $field_definition->getFieldStorageDefinition();
         $property_definitions = $storage_definition->getPropertyDefinitions();
         foreach ($property_definitions as $property_definition) {
             // Look for entity_reference properties where the storage definition
             // has a target type setting and that is enabled for content
             // translation.
             if (in_array($property_definition->getDataType(), ['entity_reference', 'entity_revision_reference']) && $storage_definition->getSetting('target_type') && $content_translation_manager->isEnabled($storage_definition->getSetting('target_type'))) {
                 // Include field if the target entity has the parent type field key
                 // set, which is defined by entity_reference_revisions.
                 $target_entity_type = \Drupal::entityTypeManager()->getDefinition($storage_definition->getSetting('target_type'));
                 if ($target_entity_type->get('entity_revision_parent_type_field')) {
                     $embeddable_fields[$field_name] = $field_definition;
                 }
             }
         }
     }
     return $embeddable_fields;
 }
コード例 #2
0
 /**
  * Returns a list of the metatag fields on an entity.
  */
 protected function getFields(ContentEntityInterface $entity)
 {
     $field_list = [];
     if ($entity instanceof ContentEntityInterface) {
         // Get a list of the metatag field types.
         $field_types = $this->fieldTypes();
         // Get a list of the field definitions on this entity.
         $definitions = $entity->getFieldDefinitions();
         // Iterate through all the fields looking for ones in our list.
         foreach ($definitions as $field_name => $definition) {
             // Get the field type, ie: metatag.
             $field_type = $definition->getType();
             // Check the field type against our list of fields.
             if (isset($field_type) && in_array($field_type, $field_types)) {
                 $field_list[$field_name] = $definition;
             }
         }
     }
     return $field_list;
 }
コード例 #3
0
 /**
  * Finds the field name for the field carrying the changed timestamp, if any.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity.
  *
  * @return string|null
  *   The name of the field found or NULL if not found.
  */
 protected function getChangedFieldName(ContentEntityInterface $entity)
 {
     foreach ($entity->getFieldDefinitions() as $field) {
         if ($field->getType() == 'changed') {
             return $field->getName();
         }
     }
 }