0

Each of my article nodes has a set of 7 paragraphs elements that must be filled in. Is there some way the client does not have to add them manually each time?

Would I create a hook that adds some kind of auto (default) paragraph entity to the paragraph field when the node is created?

Is this technically possible?

I think I have seen once a module that allows to define default paragraphs via the url.

But I think I am wrong.

  • There is the prepopulate module drupal.org/project/prepopulate but I'm not sure if it works with Pragraphs out of the box – Ismail Cherri Aug 29 '18 at 10:22
  • thanks Ismail. I found a pretty solid solution. Look at the answer. – mogio Aug 29 '18 at 15:16
1

Actually it is pretty simple. I have written a module with the following hook:

Hope that helps somebody. Pretty handy and user friendly when it comes to content that is mainly created through paragraphs elements.

You attach the field widgets to the node when the add form is opened.

function xxx_entity_prepare_form(EntityInterface $entity, $operation, FormStateInterface $form_state) {

    if("article" === $entity->bundle()){
        $bundles = ['article_navigation_banner', 'call_to_action_banner', 'author_information'];
        $entity_manager = \Drupal::entityManager();
        $langcode = $form_state->get('langcode');

        $field = $entity->get('field_ui_elements');



        $target_type = $field->getSetting('target_type');


        foreach ($bundles as $bundle_name) {
            // Create paragraph and display.
            $entity_type = $entity_manager->getDefinition($target_type);
            $bundle_key = $entity_type->getKey('bundle');


            $options = array(
                $bundle_key => $bundle_name,
                'langcode' => $langcode,
            );

            if("call_to_action_banner" === $bundle_name){

            }

            /** @var FieldableEntityInterface $paragraphs_entity */
            $paragraphs_entity = $entity_manager->getStorage($target_type)->create($options);


            // Field values.
            $field->appendItem($paragraphs_entity);
        }

    }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.