Score:0

How to add checkbox using hook_field_widget_WIDGET_TYPE_form_alter()

gb flag

Running Drupal 9 with paragraphs module, paragraphs collapsible and paragraphs jquery ui accordion installed. I wish to modify paragraphs' form, specifically where it displays private files by adding a 'Switch to Public' checkbox under each file description (see attached image).

I am using hook_field_widget_WIDGET_TYPE_form_alter(). Here is example code:

**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */

 function sbn_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
     if (!empty($element['subform']['field_private_files'])) {
         
#       $element['subform']['field_private_files']['widget'][0]['#default_value']['switch'] = [
#             '#type'          => 'checkbox',
#             '#title'         => t('Switch to Public'),
#             '#required'      => FALSE,
#             '#default_value' => FALSE,
#           ];

     $element['subform']['field_private_files']['widget'][0]['#default_value']['switch'] = array(
        '#type' => 'checkbox',
        '#title' => t('Switch to public'),
        '#size' => 10,
        '#maxlength' => 255,
        '#default_value' => 1,
        '#required' => FALSE,
        '#description' => '<p>' . t('Switch from private to public.') . '</p>',
    );
 
        // dsm($element);
     }
}

I know the array elements are there because I used dsm to print out the entire form if ['subform']['field_private_files'] exists:

   [field_private_files] => Array
        (
            [#type] => container
            [#parents] => Array
                (
                    [0] => field_body1
                    [1] => 7
                    [2] => subform
                    [3] => field_body2
                    [4] => 0
                    [5] => subform
                    [6] => field_private_files_wrapper
                )

            [#attributes] => Array
                (
                    [class] => Array
                        (
                            [0] => field--type-file
                            [1] => field--name-field-private-files
                            [2] => field--widget-file-generic
                            [3] => paragraphs-content
                        )

                )

            [widget] => Array
                (
                    [0] => Array
                        (
                            [#title] => Private Files
                            [#description] => 
                            [#field_parents] => Array
                                (
                                    [0] => field_body1
                                    [1] => 7
                                    [2] => subform
                                    [3] => field_body2
                                    [4] => 0
                                    [5] => subform
                                )

                            [#required] => 
                            [#delta] => 0
                            [#weight] => 0
                            [#type] => managed_file
                            [#upload_location] => private://
                            [#upload_validators] => Array
                                (
                                    [file_validate_size] => Array(1)
                                    [file_validate_extensions] => Array(1)
                                )

                            [#value_callback] => Array
                                (
                                    [0] => Drupal\file\Plugin\Field\FieldWidget\FileWidget
                                    [1] => value
                                )

                            [#process] => Array
                                (
                                    [0] => Array(2)
                                    [1] => Array(2)
                                )

                            [#progress_indicator] => throbber
                            [#extended] => 1
                            [#field_name] => field_private_files
                            [#entity_type] => paragraph
                            [#display_field] => 1
                            [#display_default] => 1
                            [#description_field] => 1
                            [#cardinality] => -1
                            [#default_value] => Array
                                (
                                    [target_id] => 436
                                    [display] => 1
                                    [description] => Executive Session Minutes 11-15-21
                                    [fids] => Array(1)
                                )

Any suggestions on how to code this so a checkbox would be printed under the 'description' of each Private Files section of each paragraph (there could be multiple paragraphs attached to each node). Thanks!

sonfd avatar
in flag
This should be done with a field widget for the Private Files field, not through the paragraph's form.
SomebodySysop avatar
gb flag
Can you point to an example of what you mean? It took me forever just to figure out how to use the hook with paragraphs! Not a lot of documentation on how to use out there.
sonfd avatar
in flag
https://www.drupal.org/docs/creating-custom-modules/creating-custom-field-types-widgets-and-formatters/create-a-custom-field-widget
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.