Is it possible to add the defaults to a feature?

Perhaps by simply saving the defaults in a variable using the system_settings_form, which would make it available to features via strongarm?

Comments

Sneakyvv created an issue.

cornelyus’s picture

Installed the module, everything working fine.. until I wanted to export in features the defaults of one of the paragraphs field. Because couldn't find nothing in the features to do so, just reached the issue list and found this thread.. Did a couple of tests with the suggestion above about system_settings_form and it seems to me that in order to work would have to change substancial stuff in the module.

So in the end, I'm not using the module, just "hard-coded" what I needed for now on a specific paragraph field of a form as default. Because I got it to work, maybe I can input something, and throw a couple of questions in here.

1. Is it really necessary to save in an independed table the defaults?

2. Why the need to create the defaults also as paragraphs items, and then do a "replicate" in the form_alter?

Becuase what I found it works, instead of the replicate is to instantiate a

 $paragraph = new ParagraphsItemEntity(array('field_name' => $field_name, 'bundle' => $value));

.

With this I believe saving the a variable that with an array of field_name, bundle and the defaults would suffice, while also being exportable. Hope this makes sense..

rv0’s picture

Saving as variables in Drupal does not scale well.

As defaults are entities, and entities are content in Drupal 7, there's isn't a pretty way to put them in features (perhaps some contrib module allows this?).
I'm afraid this will be a "won't fix" issue, unless someone can convince me otherwise with a patch or technical advice.

cornelyus’s picture

But, do the defaults really need to be entities? Or just the info of what entitites to create?

rv0’s picture

this was actually my initial approach when I wrote this module, but I luckily switch to using the paragraph entities pretty quick (and it made things a lot simpler)
keep in mind an entity can contain any field, and there's lots of contrib field module out there, it would probably not be easy to universally support them all with the approach you suggest.

have a look at uuid_features:
https://www.drupal.org/project/uuid_features

I think that would be part of a solution..

cornelyus’s picture

Thanks for the explanation @rv0 , you're probably right.. I was being reductive on my view because my use case is simpler than a broader solution. I just need paragraphs created with default values.. not create a default paragraph with "whatever is inside".

firewaller’s picture

This may be overkill, but since you can't save the module's defaults in features, I created an install function that can be added to a custom module in case anybody is compelled to manually export their defaults:

/**
 * Implements hook_update_N();
 */
function MY_CUSTOM_MODULE_update_7001() {
  $entity_type = 'node';
  $field_name = 'HOST_FIELD';
  $bundle_name = 'HOST_BUNDLE';
  $paragraphs_bundle = 'PARAGRAPHS_BUNDLE';
  $paragraphs_fields = array(
    'PARAGRAPHS_FIELD_1' => array(LANGUAGE_NONE => array(0 => array('value' => 'Test'))),
    'PARAGRAPHS_FIELD_2' => array(LANGUAGE_NONE => array(0 => array('value' => 'Test 2'))),
  );

  _MY_CUSTOM_MODULE_create_paragraphs_defaults($entity_type, $field_name, $bundle_name, $paragraphs_bundle, $paragraphs_fields);
}

/**
 * Helper function to create paragraphs defaults.
 */
function _MY_CUSTOM_MODULE_create_paragraphs_defaults($entity_type, $field_name, $bundle_name, $paragraphs_bundle, $paragraphs_fields = array()) {
  // Load admin form functions.
  module_load_include('inc', 'paragraphs_defaults', 'paragraphs_defaults.admin');

  // Generate form.
  $form_state = form_state_defaults();
  $form = paragraphs_defaults_edit_form(array(), $form_state, $entity_type, $field_name, $bundle_name);

  // Generate new paragraphs item entity.
  $paragraph = new ParagraphsItemEntity(array('field_name' => $field_name, 'bundle' => $paragraphs_bundle));

  // Add fields to paragraphs item entity.
  if ($paragraphs_fields) {
    foreach ($paragraphs_fields as $paragraphs_field_name => $paragraphs_field) {
      $paragraph->{$paragraphs_field_name} = $paragraphs_field;
    }
  }

  // Assign form state values.
  $field_name_value = $form['field_name']['#value'];
  $form[$field_name_value][LANGUAGE_NONE][] = array('entity' => $paragraph);

  $form_state['values'] = array(
    $field_name_value => $form[$field_name_value],
    'entity_type' => $form['entity_type']['#value'],
    'field_name' => $field_name_value,
    'bundle_name' => $form['bundle_name']['#value'],
  );

  // Submit form.
  paragraphs_defaults_edit_form_submit($form, $form_state);
}