You are here

function paragraphs_previewer_field_widget_form_process in Paragraphs Previewer 7

Process callback for the paragraph widget.

The form build id is available during the #process callbacks.

Parameters

array $element: The preview form element.

array $form_state: The form state.

array $complete_form: The complete parent form of the element.

Return value

array The updated element array.

1 string reference to 'paragraphs_previewer_field_widget_form_process'
paragraphs_previewer_field_widget_form_build in ./paragraphs_previewer.module
Implements hook_field_widget_form_build().

File

./paragraphs_previewer.module, line 191
Provides a rendered preview of a paragraphs item on an entity form..

Code

function paragraphs_previewer_field_widget_form_process($element, $form_state, $complete_form) {
  if (empty($complete_form['form_build_id']['#value'])) {
    return $element;
  }
  $form_build_id = $complete_form['form_build_id']['#value'];
  $field_parents = $element['#field_parents'];
  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $delta = $element['#delta'];
  $field_state = field_form_get_state($field_parents, $field_name, $langcode, $form_state);

  // Exit if the state instance is lost.
  if (empty($field_state['instance']['widget'])) {
    return;
  }
  $widget = $field_state['instance']['widget'];
  $item_parents = array_merge($field_parents, array(
    $field_name,
    $langcode,
    $delta,
  ));

  // Paragraph entity is available.
  if (!empty($field_state['entity'][$delta])) {
    $paragraph_item = $field_state['entity'][$delta];

    // Add preview button if enabled and not editing.
    if (empty($paragraph_item->being_edited)) {
      $preview_uri = paragraphs_previewer_form_preview_uri($item_parents, $form_build_id);
      if (!empty($preview_uri)) {
        $link_options = $preview_uri['options'] + array(
          'html' => FALSE,
          'attributes' => array(
            'target' => '_blank',
            'title' => t('Preview'),
            'class' => array(
              'button',
              'paragraphs-previewer-button',
            ),
          ),
        );

        // Set the dialog title else the title attribute is used.
        if (isset($element['paragraph_bundle_title']['info']['#markup'])) {
          $link_options['attributes']['data-dialog-title'] = strip_tags($element['paragraph_bundle_title']['info']['#markup']);
        }

        // Determine preview access.
        $preview_access = !isset($element['#access']) || !empty($element['#access']);

        // Preview button.
        $element['actions']['paragraphs_previewer_button'] = array(
          '#delta' => $delta,
          '#name' => implode('_', $item_parents) . '_paragraphs_previewer_button',
          'link' => array(
            '#theme' => 'link',
            '#text' => t('Preview'),
            '#path' => $preview_uri['path'],
            '#options' => $link_options,
          ),
          '#field_item_parents' => $item_parents,
          '#weight' => 9999,
          '#access' => $preview_access,
          '#attached' => array(
            'css' => array(
              drupal_get_path('module', 'paragraphs_previewer') . '/css/paragraphs_previewer.css',
            ),
            'js' => array(
              drupal_get_path('module', 'paragraphs_previewer') . '/js/paragraphs_previewer.js',
            ),
            'library' => array(
              array(
                'system',
                'ui.dialog',
              ),
              array(
                'system',
                'ui.progressbar',
              ),
            ),
          ),
        );
      }
    }
  }
  return $element;
}