1

I'm adding a third party settings in my .module file, in the mymodule_form_alter function. I did this is order to add an image to my paragraph.

function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  if ($form_id != "paragraphs_type_edit_form") {
    return;
  }

  //////// definition paragraphe
  $entity = $form_state->getFormObject()->getEntity();

  $third_party_settings = $entity->getThirdPartySettings('my_module');
  $illustration = $entity->getThirdPartySetting('my_module', 'paragraph_illustration');
  $form['third_party_settings']['#tree'] = TRUE;

  $form['third_party_settings']['my_module']['paragraph_illustration'] = [
    '#type' => 'managed_file',
    '#title' => 'Illustration du paragraphe',
    '#name' => $form_state->getValue('paragraph_illustration'),
    '#upload_location' => 'public://',
    '#default_value' => !empty($illustration) ? [$illustration] : NULL,
  ];

  $form['#entity_builders'][] = 'my_module_form_node_type_form_builder';

}

So far, this is working well, the settings well saved and retrived

strong text

Now, I want to display this image on my node edition, where the paragraph is added. This image is used to show what technicaly, the paragraph would render.

But for some reason, the getThirdPartySettings is always empty ([]). In the first place, I thought using the icon to display "fake preview". With the icon, it works great. But of course, the target image will be quite big, not an icon.

where is my mistake ? Should I change the hook ?

function my_module_field_widget_paragraphs_form_alter(&$element, FormStateInterface &$form_state, $context) {
  $paragraphRendering = [
    'push_produit',
  ];
  $type = $element['#paragraph_type'];
  $illustationClass = "paragraph-illustration";

  if (in_array($type, $paragraphRendering)) {
    $icon = $element['top']['type']['icon']['#uri'];
    $description = "Fake preview";
    $field_definition = $context['items']->getFieldDefinition();
    if($field_definition instanceof Drupal\field\Entity\FieldConfig) {
      $mymodule_settings = $field_definition->getThirdPartySetting('my_module', 'paragraph_illustration');
      dump($mymodule_settings);
      // dump return []
    }
    $entity = $form_state->getFormObject()->getEntity();
    $element['#prefix'] = str_replace('id="', 'class="paragraph-' . $type . '" id="', $element['#prefix']);
    $element['#prefix'] .= '<div class="' . $illustationClass . '"><p>' . $description . '</p><img src="' . $icon . '"></div>';
  }
}
5
  • Which hook is being fired first? my_module_form_alter or my_module_field_widget_paragraphs_form_alter? Also, another question: why don't you modify the form entity view display by configuration instead of using the hook?
    – jbarrio
    Jul 6, 2021 at 8:29
  • Hooks are not displayed on the same page, my_module_form_alter" is only fired on **/admin/structure/paragraphs_type/home_slider, while the other is triggered on /node/23/edit?destination=/admin/content Because I didn't knew I could do it, i'll take a look at this
    – user104792
    Jul 6, 2021 at 8:34
  • Update : I'm trying to add the third party settings in my form display
    – user104792
    Jul 6, 2021 at 8:49
  • Hooks not being displayed on the same page is exactly the reason you are not getting any result, because unless you save somehow the setting you are adding on the fly via hook_form_alter, once submitted, it gets lost and you cannot query it on a different place.
    – jbarrio
    Jul 6, 2021 at 13:51
  • what is the procedure to follow so ?
    – user104792
    Jul 6, 2021 at 15:06

1 Answer 1

0

You're adding a 3rd party setting to a paragraph type entity, but trying to retrieve it from a field entity. You need to retrieve it from the same entity you set it on, e.g.

function my_module_field_widget_paragraphs_form_alter(&$element, FormStateInterface &$form_state, $context) {
  ...
  $setting = $form_state->get('paragraph')->getParagraphType()
    ->getThirdPartySetting('my_module', 'paragraph_illustration');
  ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.