I’m trying to set a text list field value when a form is loaded but can’t seem to get the following code to work. As the value has dependencies I’m not able to use hook node form alter or use the default value with form state.
use DrupalnodeEntityNode;
use DrupalCoreFormFormStateInterface;
use DrupalCoreEntityEntityInterface;
use DrupalCoreEntityFieldableEntityInterface;
/**
* Implements hook_entity_prepare_form().
*
*/
function site_hours_answer_prepare_form(EntityInterface $entity, $operation, FormStateInterface $form_state)
{
if ($entity instanceof FieldableEntityInterface) {
$entity->set('field_question_type', 'multiple_choice');
}
}
Any suggestions would be awesome.
I’ve been using the Entity Pre-populate (EPP) project as a guide as it uses tokens and the prepare form hook but I’ve had no luck. I’ve also searched the forums high and low but can’t find much about this hook.
davidp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.