You are here

abstract class ResourceTypeField in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/ResourceType/ResourceTypeField.php \Drupal\jsonapi\ResourceType\ResourceTypeField

Abstract value object containing all metadata for a JSON:API resource field.

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

Hierarchy

Expanded class hierarchy of ResourceTypeField

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

\Drupal\jsonapi\ResourceType\ResourceTypeRepository

1 file declares its use of ResourceTypeField
EntityResource.php in core/modules/jsonapi/src/Controller/EntityResource.php

File

core/modules/jsonapi/src/ResourceType/ResourceTypeField.php, line 16

Namespace

Drupal\jsonapi\ResourceType
View source
abstract class ResourceTypeField {

  /**
   * The internal field name.
   *
   * @var string
   */
  protected $internalName;

  /**
   * The public field name.
   *
   * @var string
   */
  protected $publicName;

  /**
   * Whether the field is disabled.
   *
   * @var bool
   */
  protected $enabled;

  /**
   * Whether the field can only have one value.
   *
   * @var bool
   */
  protected $hasOne;

  /**
   * ResourceTypeField constructor.
   *
   * @param string $internal_name
   *   The internal field name.
   * @param string $public_name
   *   (optional) The public field name. Defaults to the internal field name.
   * @param bool $enabled
   *   (optional) Whether the field is enabled. Defaults to TRUE.
   * @param bool $has_one
   *   (optional) Whether the field can only have ony value. Defaults to TRUE.
   */
  public function __construct($internal_name, $public_name = NULL, $enabled = TRUE, $has_one = TRUE) {
    $this->internalName = $internal_name;
    $this->publicName = $public_name ?: $internal_name;
    $this->enabled = $enabled;
    $this->hasOne = $has_one;
  }

  /**
   * Gets the internal name of the field.
   *
   * @return string
   *   The internal name of the field.
   */
  public function getInternalName() {
    return $this->internalName;
  }

  /**
   * Gets the public name of the field.
   *
   * @return string
   *   The public name of the field.
   */
  public function getPublicName() {
    return $this->publicName;
  }

  /**
   * Establishes a new public name for the field.
   *
   * @param string $public_name
   *   The public name.
   *
   * @return static
   *   A new instance of the field with the given public name.
   */
  public function withPublicName($public_name) {
    return new static($this->internalName, $public_name, $this->enabled, $this->hasOne);
  }

  /**
   * Gets a new instance of the field that is disabled.
   *
   * @return static
   *   A new instance of the field that is disabled.
   */
  public function disabled() {
    return new static($this->internalName, $this->publicName, FALSE, $this->hasOne);
  }

  /**
   * Whether the field is enabled.
   *
   * @return bool
   *   Whether the field is enabled. FALSE if the field should not be in the
   *   JSON:API response.
   */
  public function isFieldEnabled() {
    return $this->enabled;
  }

  /**
   * Whether the field can only have one value.
   *
   * @return bool
   *   TRUE if the field can have only one value, FALSE otherwise.
   */
  public function hasOne() {
    return $this->hasOne;
  }

  /**
   * Whether the field can have many values.
   *
   * @return bool
   *   TRUE if the field can have more than one value, FALSE otherwise.
   */
  public function hasMany() {
    return !$this->hasOne;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResourceTypeField::$enabled protected property Whether the field is disabled.
ResourceTypeField::$hasOne protected property Whether the field can only have one value.
ResourceTypeField::$internalName protected property The internal field name.
ResourceTypeField::$publicName protected property The public field name.
ResourceTypeField::disabled public function Gets a new instance of the field that is disabled. 1
ResourceTypeField::getInternalName public function Gets the internal name of the field.
ResourceTypeField::getPublicName public function Gets the public name of the field.
ResourceTypeField::hasMany public function Whether the field can have many values.
ResourceTypeField::hasOne public function Whether the field can only have one value.
ResourceTypeField::isFieldEnabled public function Whether the field is enabled.
ResourceTypeField::withPublicName public function Establishes a new public name for the field. 1
ResourceTypeField::__construct public function ResourceTypeField constructor.