You are here

public function CommentFieldItemList::get in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/src/CommentFieldItemList.php \Drupal\comment\CommentFieldItemList::get()

Returns the item at the specified position in this list.

Parameters

int $index: Index of the item to return.

Return value

\Drupal\Core\TypedData\TypedDataInterface|null The item at the specified position in this list, or NULL if no item exists at that position.

Throws

\Drupal\Core\TypedData\Exception\MissingDataException If the complex data structure is unset and no item can be created.

Overrides ItemList::get

File

core/modules/comment/src/CommentFieldItemList.php, line 17

Class

CommentFieldItemList
Defines an item list class for comment fields.

Namespace

Drupal\comment

Code

public function get($index) {

  // The Field API only applies the "field default value" to newly created
  // entities. In the specific case of the "comment status", though, we need
  // this default value to be also applied for existing entities created
  // before the comment field was added, which have no value stored for the
  // field.
  if ($index == 0 && empty($this->list)) {
    $field_default_value = $this
      ->getFieldDefinition()
      ->getDefaultValue($this
      ->getEntity());
    return $this
      ->appendItem($field_default_value[0]);
  }
  return parent::get($index);
}