You are here

public function FieldCollectionItemController::add in Field collection 8.3

Same name and namespace in other branches
  1. 8 src/Controller/FieldCollectionItemController.php \Drupal\field_collection\Controller\FieldCollectionItemController::add()

Provides the field collection item submission form.

Parameters

\Drupal\field_collection\Entity\FieldCollection $field_collection: The field_collection entity for the field collection item.

$host_type: The type of the entity hosting the field collection item.

$host_id: The id of the entity hosting the field collection item.

Return value

array A field collection item submission form.

TODO: additional fields

1 string reference to 'FieldCollectionItemController::add'
field_collection.routing.yml in ./field_collection.routing.yml
field_collection.routing.yml

File

src/Controller/FieldCollectionItemController.php, line 32

Class

FieldCollectionItemController
Returns responses for Field collection item routes.

Namespace

Drupal\field_collection\Controller

Code

public function add(FieldCollection $field_collection, $host_type, $host_id) {
  $host = $this
    ->entityTypeManager()
    ->getStorage($host_type)
    ->load($host_id);
  if (_field_collection_field_item_list_full($host->{$field_collection
    ->id()})) {
    drupal_set_message(t('This field is already full.'), 'error');
    return [
      '#markup' => 'Can not add to an already full field.',
    ];
  }
  else {
    $field_collection_item = $this
      ->entityTypeManager()
      ->getStorage('field_collection_item')
      ->create([
      'field_name' => $field_collection
        ->id(),
      'host_type' => $host_type,
      'revision_id' => 0,
    ]);
    $form = $this
      ->entityFormBuilder()
      ->getForm($field_collection_item);
    return $form;
  }
}