Hi,

In order to truly replace field collection (i want to avoid using that module alongside this one...), it would be great if the content creator did not need to click 'add a paragraph' in circumstances where this is a pointless step.

For example, say you
- add a paragraph field
- allow one paragraph bundle type only
- set the field cardinality (ie number of values) to 1

When the node form loads in this circumstance, you just want to see the fields without an add/remove paragraph button, since it effectively serves no purpose. Would this be difficult to achieve?

I'm happy to have a go at a patch, but if you could give any pointers as to the best way to approach this it would be a great help.

cheers

Comments

njbarrett’s picture

This is certainly something I'd like to see.
I am using only one paragraph bundle and have a set cardinality of 4.
I would prefer all the paragraph fields to show rather than having to click a button four times to add them.

jeroen.b’s picture

Status: Active » Needs work

I'd like to make everything configurable through the widget settings form.
So best would be to add an option: pre-create x (numeric field) items

themodularlab’s picture

I recently ran into a similar need related to Paragraphs. I have a custom module that was limited to 4 paragraph item values, but I needed all 4 to be present on form load rather than clicking the add more until I reach the field's limit. I was finally able to solve this. It certainly was not the most elegant way and I'm sure there is much better cleaner way to do this but this is what I did to make this work. FYI, I have only tested this with limited values, although it should still use the "add more" button if the cardinality is set to unlimited. It should also be noted that I'm working with Paragraphs version: '8.x-1.1'.

I made a copy of the InlineParagraphsWidget (/paragraphs/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php) and place it in my module. I didn't need any widget settings for this so I just removed them. Of course, if you need the settings, then just adjust accordingly or make a new add_mode option.

Next I had to dig around into Drupal core to find this, but I went into the core widgetBase (/core/lib/Drupal/Core/Field/WidgetBase.php and found the following:

// Determine the number of widgets to display.
     switch ($cardinality) {
       case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
         $field_state = static::getWidgetState($this->fieldParents, $field_name, $form_state);
         $max = $field_state['items_count'];
         $is_multiple = TRUE;
         break;

       default:
         $max = $cardinality;
         $is_multiple = ($cardinality > 1);
         break;
     }

I placed that in the public function formMultipleElements().

and finally, on line 900 of the original paragraph widget, I had to update the IF statement

if (($this->realItemCount < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state->isProgrammed() && !$this->isTranslating) {}

to

if (($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state->isProgrammed() && !$this->isTranslating) {}

by removing the $this->realItemCount < $cardinality || portion of the statement.

Again, I'm sure there is a better way to do this but it's at least a starting point that will allow you to show all the limited field value options by default. I hope this helps.

hkirsman’s picture

Well I'm looking for a functionality not add the paragraph by default. I have different banner fields that refer to paragraph. By default I'd like them to be disabled and then user can add them if necessary. Right now as I have only 1 paragraph and cardinality 1 they all show up on node creation.

PS. Didn't notice it's a D7 issue but the problem is still the same :)

unknown2’s picture

I want the functionality of displaying 10-15 paragraph items by default in the content type where paragraph is used as reference. how can i achieve that

miro_dietiker’s picture

@unknown2 Either by cloning a node with these Paragraphs or by using something like Paragraph Sets - but you still need to push a button. Or by writing a few lines of code.

thamas’s picture

Version: 7.x-1.0-beta5 » 8.x-1.x-dev
thamas’s picture

Maybe https://www.drupal.org/project/default_paragraphs can (partly) solve the original issue(?). (Have not tried yet…)