Account created on 12 January 2008, almost 17 years ago
#

Recent comments

๐Ÿ‡บ๐Ÿ‡ธUnited States rkeppner

Following up on this, it seems that the inline_entity_form issue referenced above was resolved almost 5 years ago. Is this a feature that could possibly be addressed now? Our content workflow kind of hinges on being able to make revisions to an existing page and have them approved before publishing the new version, and I can't really see a way of accomplishing that without widget revisions.

๐Ÿ‡บ๐Ÿ‡ธ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;
    }
  }
}

๐Ÿ‡บ๐Ÿ‡ธUnited States rkeppner

rkeppner โ†’ made their first commit to this issueโ€™s fork.

๐Ÿ‡บ๐Ÿ‡ธUnited States rkeppner

After finding the working endpoint in my version of Drupal but finding this documentation that stated it wasn't yet in core, I verified the change and updated the docs.

Production build 0.71.5 2024