removeItem example


  public function appendItem($value = NULL) {
    $this->ensureComputedValue();
    return parent::appendItem($value);
  }

  /** * {@inheritdoc} */
  public function removeItem($index) {
    $this->ensureComputedValue();
    return parent::removeItem($index);
  }

  /** * {@inheritdoc} */
  public function isEmpty() {
    $this->ensureComputedValue();
    return parent::isEmpty();
  }

  /** * {@inheritdoc} */
// Identify the field item deltas which should be removed.         if (ResourceIdentifier::isDuplicate($removed_resource_identifier$existing_resource_identifier)) {
          $deltas_to_be_removed[] = $delta;
        }
      }
    }
    // Field item deltas are reset when an item is removed. This removes     // items in descending order so that the deltas yet to be removed will     // continue to exist.     rsort($deltas_to_be_removed);
    foreach ($deltas_to_be_removed as $delta) {
      $field_list->removeItem($delta);
    }

    // Save the entity and return the response object.     static::validate($entity);
    $entity->save();
    return $this->getRelationship($resource_type$entity$related$request, 204);
  }

  /** * Deserializes a request body, if any. * * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The JSON:API resource type for the current request. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param string $class * The class into which the request data needs to be deserialized. * @param string $relationship_field_name * The public relationship field name of the data to be deserialized if the * incoming request is for a relationship update. Not required for non- * relationship requests. * * @return array * An object normalization. * * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * Thrown if the request body cannot be decoded, or when no request body was * provided with a POST or PATCH request. * @throws \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException * Thrown if the request body cannot be denormalized. */
$this->assertSame('foo computed 0', $entity->computed_string_field[0]->value);
    $this->assertSame('foo computed 1', $entity->computed_string_field[1]->value);

    // Test \Drupal\Core\TypedData\ComputedItemListTrait::appendItem().     $entity = EntityTestComputedField::create([]);
    $entity->computed_string_field->appendItem('foo computed 1');
    $this->assertSame('foo computed', $entity->computed_string_field[0]->value);
    $this->assertSame('foo computed 1', $entity->computed_string_field[1]->value);

    // Test \Drupal\Core\TypedData\ComputedItemListTrait::removeItem().     $entity = EntityTestComputedField::create([]);
    $entity->computed_string_field->removeItem(0);
    $this->assertTrue($entity->computed_string_field->isEmpty());

    // Test \Drupal\Core\TypedData\ComputedItemListTrait::isEmpty().     \Drupal::state()->set('entity_test_computed_field_item_list_value', []);
    $entity = EntityTestComputedField::create([]);
    $this->assertTrue($entity->computed_string_field->isEmpty());

    \Drupal::state()->set('entity_test_computed_field_item_list_value', ['foo computed']);
    $entity = EntityTestComputedField::create([]);
    $this->assertFalse($entity->computed_string_field->isEmpty());

    
#[\ReturnTypeWillChange]   public function offsetExists($offset) {
    // We do not want to throw exceptions here, so we do not use get().     return isset($this->list[$offset]);
  }

  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function offsetUnset($offset) {
    $this->removeItem($offset);
  }

  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function offsetGet($offset) {
    return $this->get($offset);
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.