4

I would like to edit a field in the node_form of a paragraph item. For normal fields i would use something like the code below to tweak a field called field_p_custom_id (d7)

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        // tweak CONTENTTYPE_node_form 
        case 'CONTENTTYPE_node_form':
            // hide custom ID field for all users, even admin (this field should never be changed)
            $form['field_p_custom_id']['#access'] = FALSE;

        break;
    }
}

... but, in the case of a paragraph item, the field_p_custom_id is placed inside a paragraph bundle. I searched inside $form and $form_state, but can't find where to alter this field. (the paragraph item is default not present on the form since it is not required and can be added unlimited times)

  • I just ran into the same issue and have not been able to sort this out. – Danny Englander Aug 3 '17 at 20:24
1

I have been struggling with this for the last two days. I was looking to add a button that would allow users to enter an ASIN and click the button, which would get a bunch of information from the Amazon API. I wanted to use some sort of hook to place the button and some additional containers for the info from the API call directly into the paragraph. However, I could not find a way to do that. I ended up building a solution that works, but is not quite the Drupal way.

I used hook_form_FORM_ID_alter() to insert a jQuery ajaxComplete command in the page that fired after someone clicked on the "add new paragraph" button. In the callback function, I inserted my button with a click handler in the new paragraph form (with some checks so that my button only got built once each time the user clicked the "add new paragraph" button).

The click handler then made its own ajax call, and I used a hook_menu to catch that and take care of the Amazon API call. Finally, I put together a bunch of hacky-feeling jQuery calls to create divs and insert the Amazon information in them.

It is not very elegant, but my code is below if it will help anyone.

I did find that someone built/patched the hooks we are looking for to paragraphs in Drupal 8. Too bad we don't have them for Drupal 7. Hopefully someone can point out a better solution and/or create the hooks in the future.

<?php

function amazon_fill_form_amazon_list_node_form_alter(&$form, &$form_state, $form_id) {

$form['from'] = array(

    '#type' => 'item',

    '#markup' => '  
        <script>
            var beer = function() {
                event.preventDefault();
                var ASIN = jQuery(this).parent().parent().find("input").val();
                var preThis = this;
                jQuery.get("/bestpresentsfor.com/getamazoninfo/?asin=" + ASIN, function(info) {
                    console.log(info);
                    var paragraphContainer = jQuery(preThis).closest(".ajax-new-content");
                    jQuery(paragraphContainer).find(".field-name-field-title").find(".description").after("<div class=\"asin-title-container\">This is the title that Amazon provides:<br>" + info[ASIN].title + "</div>");
                    jQuery(paragraphContainer).find(".field-type-link-field").find("input").val(info[ASIN].detailpageurl);
                    jQuery(paragraphContainer).find(".field-name-field-product-image").find(".description").after("<div class=\"asin-image-container\">This is the image that Amazon provides:<br><img src=\" " + info[ASIN].imagesets.largeimage.url + " \" alt=\"There is no Amazon image for this product!\"></div>");
                    jQuery(paragraphContainer).find(".field-name-field-product-description-amazon").find(".description").after("<div class=\"asin-description-container\">This is the description that Amazon provides:<br>" + info[ASIN].editorialreviews[0].content + "</div>");
                });
                return false;
            }

            var megaIndex = 0;
            var newButton = "<div class=\"asin-button-container\"><button class=\"asin-button\" onclick=\"beer.call(this)\">Enter ASIN</button></div>";
            jQuery( document ).ajaxComplete(function() {
                addASINS = jQuery(".field-type-asin");
                for (var i = 0; i < addASINS.length; i++) {
                    if (jQuery(addASINS[i]).find(".asin-button").length == 0) {
                        jQuery(addASINS[i]).append(newButton);
                    }
                }
            })
        </script>
    ',
);
}

function amazon_fill_menu() {
     $items['getamazoninfo'] = array(
         'page callback' => 'page_callback', 
         'type' => MENU_CALLBACK,
         'access arguments' => array('access content'),
     );
     return $items;
}

function page_callback() {
     $asin = $_GET['asin'];
     $info = amazon_item_lookup_from_web($item_ids = array( $asin ));
     drupal_json_output(
         $info
     );
     drupal_exit(); 
}
1

For Drupal 8 you can implement hook_field_widget_WIDGET_TYPE_form_alter.

/**  
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().  
 */  
function paragraphs_test_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
  if ($element['#paragraph_type'] == 'altered_paragraph') {
    $element['subform']['field_text']['widget'][0]['#title'] = 'Altered title';
  }
} 

see my answer here: How do I alter the behavior of the form fields contained within the paragraph field?

0

When create a new node the paragraph field always will be empty, you can try creating a node and add some paragraph, then edit the node and search inside $form. Maybe you can add a default paragraphs first https://www.drupal.org/project/paragraphs_defaults

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.