withPublicName example

/** * Sets the public name of the given field on the resource type to be built. * * @param \Drupal\jsonapi\ResourceType\ResourceTypeField $field * The field for which to set a public name. * @param string $public_field_name * The public field name to set. */
  public function setPublicFieldName(ResourceTypeField $field$public_field_name) {
    foreach ($this->fields as $index => $value) {
      if ($field === $value) {
        $this->fields[$index] = $value->withPublicName($public_field_name);
        return;
      }
    }
  }

  /** * Disables the given field on the resource type to be built. * * @param \Drupal\jsonapi\ResourceType\ResourceTypeField $field * The field for which to set a public name. */
  
public function getRelatableResourceTypes() {
    if (!isset($this->relatableResourceTypes)) {
      throw new \LogicException("withRelatableResourceTypes() must be called before getting relatable resource types.");
    }
    return $this->relatableResourceTypes;
  }

  /** * {@inheritdoc} */
  public function withPublicName($public_name) {
    $relationship = parent::withPublicName($public_name);
    return isset($this->relatableResourceTypes)
      ? $relationship->withRelatableResourceTypes($this->relatableResourceTypes)
      : $relationship;
  }

  /** * {@inheritdoc} */
  public function disabled() {
    $relationship = parent::disabled();
    return isset($this->relatableResourceTypes)
      ?

class AliasingResourceTypeRepository extends ResourceTypeRepository {

  /** * {@inheritdoc} */
  protected function getFields(array $field_names, EntityTypeInterface $entity_type$bundle) {
    $fields = parent::getFields($field_names$entity_type$bundle);
    foreach ($fields as $field_name => $field) {
      if (str_starts_with($field_name, 'field_test_alias_')) {
        $fields[$field_name] = $fields[$field_name]->withPublicName('field_test_alias');
      }
    }
    return $fields;
  }

}
Home | Imprint | This part of the site doesn't use cookies.