How to preview the edited content widget ?

Created on 18 July 2018, about 7 years ago
Updated 10 October 2024, 11 months ago

While editing content, it is not clear which widget has which content(!)
This kind of content editing is not very practical. How can I preview the widget while editing the content?

πŸ’¬ Support request
Status

Active

Version

1.0

Component

User interface

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States rkeppner

    I ended up solving this with with a custom module, borrowing lots of code from StepOne.php:

    
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\stacks\Widget\WidgetTemplates;
    use Drupal\stacks\Entity\WidgetInstanceEntity;
    
    /**
     * Implements hook_form_alter().
     */
    function gcu_stacks_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      if (isset($form['field_stack_widgets']['widget'])) {
        // Get stacks widgets.
        $widgets = &$form['field_stack_widgets']['widget'];
    
        foreach ($widgets as $delta => $widget) {
          if (is_array($widget) && array_key_exists('widget_instance_id', $widget)) {
            $widget_instance_id = $widget['widget_instance_id']['#default_value'];
            $widget_instance = WidgetInstanceEntity::load($widget_instance_id);
    
            if (empty($widget_instance)) {
              continue;
            }
    
            // Add the preview image.
            $preview_image = gcu_stacks_get_widget_instance_image($widget_instance);
            if (!empty($preview_image)) {
              // Insert the preview image into the form, before the delete button.
              $keys = array_keys($form['field_stack_widgets']['widget'][$delta]['data']);
              $position = array_search('delete', $keys);
    
              $before = array_slice($form['field_stack_widgets']['widget'][$delta]['data'], 0, $position, TRUE);
              $after = array_slice($form['field_stack_widgets']['widget'][$delta]['data'], $position, NULL, TRUE);
    
              $before['preview_image'] = ['#markup' => $preview_image];
              $form['field_stack_widgets']['widget'][$delta]['data'] = $before + $after;
            }
          }
        }
      }
    }
    
    function gcu_stacks_get_widget_instance_image($widget_instance) {
      $entity = $widget_instance->getWidgetEntity();
      $type = $entity->getType();
      $templates = WidgetTemplates::getTemplatesSelect();
      $base_dir_stacks = WidgetTemplates::templateDir();
    
      foreach ($templates as $bundle => $bundle_templates) {
        // Continue if this is not the widget type we are looking for.
        if ($bundle != $type) {
          continue;
        }
    
        foreach ($bundle_templates as $bundle_template => $bundle_template_display) {
          // Continue if this is not the bundle template we are looking for.
          if ($widget_instance->getTemplate() != $bundle_template) {
            continue;
          }
    
          // Define the preview image.
          $renamed_dir = str_replace(['_', ' '], '-', $bundle);
          $renamed_template = str_replace(['_', ' '], '-', $bundle_template);
          $preview_image_file = DRUPAL_ROOT . '/' . $base_dir_stacks . '/' . $renamed_dir . '/images/' . $renamed_template;
          $preview_images = array_merge(glob($preview_image_file . '.{jpg,jpeg,png,gif}', GLOB_BRACE), glob($preview_image_file . '.{JPG,JPEG,PNG,GIF}', GLOB_BRACE));
    
          if (!empty($preview_images)) {
            $preview_image_file = substr($preview_images[0], strlen(DRUPAL_ROOT));
            $preview_image_array = [
              '#type' => 'container',
              'img' => [
                '#type' => 'html_tag',
                '#tag' => 'img',
                '#attributes' => [
                  'width' => 241,
                  'src' => $preview_image_file,
                  'title' => $bundle_template_display,
                  'class' => ['preview-image'],
                ],
              ],
            ];
          }
          else {
            $preview_image_array = [
              '#type' => 'container',
              'img' => [
                '#type' => 'html_tag',
                '#tag' => 'img',
                '#attributes' => [
                  'width' => 241,
                  'height' => 234,
                  'title' => '',
                  'alt' => '',
                  'src' => '/' . \Drupal::service('extension.list.module')->getPath('stacks') . '/images/no-preview-img.png',
                  'class' => ['preview-image'],
                ],
              ],
            ];
          }
    
          $preview_image = \Drupal::service('renderer')->render($preview_image_array);
    
          return $preview_image;
        }
      }
    }
    
    
Production build 0.71.5 2024