🇫🇮Finland @MikaT

Account created on 17 October 2013, about 11 years ago
#

Recent comments

🇫🇮Finland MikaT

Patch to fix WebformSubmissionWorkflowListBuilder::getQuery

  • inherit comment
  • added return statement
🇫🇮Finland MikaT

To me this seems that in WebformCompositeBase::validateWebformComposite the $composite_elements is empty because the value is missing in the WebformWorkflowsElement::getCompositeElements $element?

From webform_workflows_element/src/Element/WebformWorkflowsElement.php

if (!isset($element['#value'])) {
  return $elements;
}
🇫🇮Finland MikaT

Note that you cannot use the Database::setActiveConnection for replica usage when using entity load like load or loadMultiple. If you have custom entities you can edit the entity storage class and set the database replica connection to database variable. Example

class MyCustomEntityStorage extends SqlContentEntityStorage {
  /**
   * Use replica for the load multiple.
   *
   * @param array|null $ids
   *   Optional list of entity ID's.
   *
   * @return array
   *   Result of the loadMultiple.
   */
  public function replicaLoadMultiple(array $ids = NULL): array {
    // Storage the original database connection.
    $origDatabase = $this->database;

    // Set the handler to use replica as database.
    $this->database = \Drupal::service('database.replica');

    // Execute data loading.
    $result = parent::loadMultiple($ids);

    // Set database back to original.
    $this->database = $origDatabase;

    return $result;
  }
}
🇫🇮Finland MikaT

Note that the custom properties doesn't seem to support tokens so one might need to extend the WebformManagedFileBase and in that new class override the getUploadLocation.

As I needed token support, I did it so that I created new WeformElement extending the WebformManagedFileBase that introduced new field where the upload location can be entered. Then in getUploadLocation get the new field value from $element, run $this->replaceTokens($element) and then call the parent getUploadLocation.


namespace Drupal\my_module\Plugin\WebformElement;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElement\WebformManagedFileBase;
use Drupal\webform\WebformInterface;

/**
 * Custom version of the webform document file.
 *
 * @WebformElement(
 *   id = "custom_webform_managed_file",
 *   label = @Translation("Custom managed file"),
 *   description = @Translation("Custom version of the webform managed file."),
 *   category = @Translation("File upload elements"),
 *   states_wrapper = TRUE,
 *   dependencies = {
 *     "file",
 *   }
 * )
 */
class CustomManagedFile extends WebformManagedFileBase {

  /**
   * {@inheritdoc}
   */
  protected function defineDefaultProperties() {
    return parent::defineDefaultProperties() + [
      'file_directory' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    $form['file']['file_directory'] = [
      '#type' => 'textfield',
      '#title' => $this->t('File directory'),
      '#description' => $this->t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function getUploadLocation(array $element, WebformInterface $webform) {
    if (empty($element['#file_directory'])) {
      return parent::getUploadLocation($element, $webform);
    }
    $uriScheme = $this->getUriScheme($element);
    // Replace tokens.
    $this->replaceTokens($element);
    // Alter the element so we can call parent.
    $element['#upload_location'] = $uriScheme . '://' . $element['#file_directory'];

    return parent::getUploadLocation($element, $webform);
  }

}

Remember to create an element matching the ID of the webform element

<?php

namespace Drupal\ssp_site\Element;

use Drupal\webform\Element\WebformManagedFileBase;

/**
 * Provides a webform element for an 'custom_webform_managed_file' element.
 *
 * @FormElement("custom_webform_managed_file")
 */
class CustomManagedFile extends WebformManagedFileBase {

}

Production build 0.71.5 2024