I have done custom code to add value for field but its throwing error which is given below.

Code is given below-
$mynode = node_load(983);
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_test'));
$field_collection_item->field_end_date[LANGUAGE_NONE][0]['value'] = "20-20-2015";
$field_collection_item->field_account_name[LANGUAGE_NONE][0]['value'] = "DCX";

// Save field collection item
$field_collection_item->setHostEntity($mynode);
$field_collection_item->save(TRUE);

I got this Error-

The website encountered an unexpected error. Please try again later.

InvalidArgumentException: Unable to get a value with a non-numeric delta in a list. in Drupal\Core\TypedData\Plugin\DataType\ItemList->get() (line 103 of core\lib\Drupal\Core\TypedData\Plugin\DataType\ItemList.php).

Drupal\Core\TypedData\Plugin\DataType\ItemList->offsetGet('LANGUAGE_NONE')
csvimport_import_batch_processing(Array, Array)
call_user_func_array('csvimport_import_batch_processing', Array)
_batch_process(Array)
_batch_progress_page()
_batch_page(Object)
Drupal\system\Controller\BatchController->batchPage(Object)
call_user_func_array(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
Stack\StackedHttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)

Comments

Pratiksha28 created an issue. See original summary.

jmuzz’s picture

Status: Active » Postponed (maintainer needs more info)

I don't think we need to support entity_create as it is deprecated. Can you try it with FieldCollectionItem::create instead? There's some examples of this in the tests:

src/Tests/FieldCollectionBasicTestCase.php

jmuzz’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)
sagesolutions’s picture

Instead of

$field_collection_item->field_end_date[LANGUAGE_NONE][0]['value'] = "20-20-2015";

try using

$field_collection_item->field_end_date->value = "20-20-2015";