mainPropertyName example

$field_access = $field_list->access('view', NULL, TRUE);
        if (!$field_access->isAllowed()) {
          $message = 'The current user is not allowed to view this relationship.';
          $exception = new EntityAccessDeniedHttpException($field_list->getEntity()$field_access, '', $message$public_field_name);
          $includes = IncludedData::merge($includesnew IncludedData([$exception]));
          continue;
        }
        $target_type = $field_list->getFieldDefinition()->getFieldStorageDefinition()->getSetting('target_type');
        assert(!empty($target_type));
        foreach ($field_list as $field_item) {
          assert($field_item instanceof EntityReferenceItem);
          $references[$target_type][] = $field_item->get($field_item::mainPropertyName())->getValue();
        }
      }
      foreach ($references as $target_type => $ids) {
        $entity_storage = $this->entityTypeManager->getStorage($target_type);
        $targeted_entities = $entity_storage->loadMultiple(array_unique($ids));
        $access_checked_entities = array_map(function DEntityInterface $entity) {
          return $this->entityAccessChecker->getAccessCheckedResourceObject($entity);
        }$targeted_entities);
        $targeted_collection = new IncludedData(array_filter($access_checked_entitiesfunction DResourceIdentifierInterface $resource_object) {
          return !$resource_object->getResourceType()->isInternal();
        }));
        
$field_item_definition = $field_item_list->getFieldDefinition();

    $values = [];
    foreach ($field_item_list as $field_item) {
      /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
      if ($field) {
        $values[] = $field_item->$field;
      }
      // Find the value using the main property of the field. If no main       // property is provided fall back to 'value'.       elseif ($main_property_name = $field_item->mainPropertyName()) {
        $values[] = $field_item->{$main_property_name};
      }
      else {
        $values[] = $field_item->value;
      }
    }
    if ($field_item_definition->getFieldStorageDefinition()->getCardinality() == 1) {
      return reset($values);
    }
    else {
      return $values;
    }
public function getTitle(): ?string {
    return $this->title ?: NULL;
  }

  /** * {@inheritdoc} */
  public function setValue($values$notify = TRUE) {
    // Treat the values as property value of the main property, if no array is     // given.     if (isset($values) && !is_array($values)) {
      $values = [static::mainPropertyName() => $values];
    }
    if (isset($values)) {
      $values += [
        'options' => [],
      ];
    }
    parent::setValue($values$notify);
  }

}

  public function getPropertyNames() {
    return array_keys($this->getPropertyDefinitions());
  }

  /** * {@inheritdoc} */
  public function getMainPropertyName() {
    $class = $this->getFieldItemClass();
    return $class::mainPropertyName();
  }

  /** * {@inheritdoc} */
  public function getUniqueStorageIdentifier() {
    return $this->uuid();
  }

  /** * Helper to retrieve the field item class. */
// Test sample item generation.     /** @var \Drupal\entity_test\Entity\EntityTest $entity */
    $entity = EntityTest::create();
    $entity->comment->generateSampleItems();
    $this->entityValidateAndSave($entity);
    $this->assertContains($entity->get('comment')->status, [
      CommentItemInterface::HIDDEN,
      CommentItemInterface::CLOSED,
      CommentItemInterface::OPEN,
    ], 'Comment status value in defined range');

    $mainProperty = $entity->comment[0]->mainPropertyName();
    $this->assertEquals('status', $mainProperty);
  }

  /** * Tests comment author name. */
  public function testCommentAuthorName() {
    $this->installEntitySchema('comment');
    $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');

    $host = EntityTest::create(['name' => $this->randomString()]);
    

  public function getPropertyNames() {
    return array_keys($this->getPropertyDefinitions());
  }

  /** * {@inheritdoc} */
  public function getMainPropertyName() {
    $class = $this->getItemDefinition()->getClass();
    return $class::mainPropertyName();
  }

  /** * {@inheritdoc} */
  public function __sleep() {
    // Do not serialize the statically cached property definitions.     $vars = get_object_vars($this);
    unset($vars['propertyDefinitions']$vars['typedDataManager']);
    return array_keys($vars);
  }

  
$source_field = $this->configuration['source_field'];
    if (empty($source_field)) {
      throw new \RuntimeException('Source field for media source is not defined.');
    }

    $items = $media->get($source_field);
    if ($items->isEmpty()) {
      return NULL;
    }

    $field_item = $items->first();
    return $field_item->{$field_item->mainPropertyName()};
  }

  /** * {@inheritdoc} */
  public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
    $display->setComponent($this->getSourceFieldDefinition($type)->getName()[
      'label' => 'visually_hidden',
    ]);
  }

  
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
    $values = [];
    $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY] = new CacheableMetadata();
    if (!empty($field_item->getProperties(TRUE))) {
      // We normalize each individual value, so each can do their own casting,       // if needed.       $field_properties = TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item);
      foreach ($field_properties as $property_name => $property) {
        $values[$property_name] = $this->serializer->normalize($property$format$context);
      }
      // Flatten if there is only a single property to normalize.       $flatten = count($field_properties) === 1 && $field_item::mainPropertyName() !== NULL;
      $values = static::rasterizeValueRecursive($flatten ? reset($values) : $values);
    }
    else {
      $values = $field_item->getValue();
    }
    $normalization = new CacheableNormalization(
      $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY],
      $values
    );
    unset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
    return $normalization;
  }

  public function testMainProperty() {
    // Let's enable all Drupal modules in Drupal core, so we test any field     // type plugin.     $this->enableAllCoreModules();

    /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    foreach ($field_type_manager->getDefinitions() as $plugin_id => $definition) {
      $class = $definition['class'];
      $property = $class::mainPropertyName();
      if ($property === NULL) {
        continue;
      }
      $storage_definition = BaseFieldDefinition::create($plugin_id);
      $property_definitions = $class::propertyDefinitions($storage_definition);
      $properties = implode(', ', array_keys($property_definitions));
      if (!empty($property_definitions)) {
        $message = sprintf("%s property %s found in %s", $plugin_id$property$properties);
        $this->assertArrayHasKey($property$class::propertyDefinitions($storage_definition)$message);
      }
    }
  }
Home | Imprint | This part of the site doesn't use cookies.