You are here

fillpdf_test.module in FillPDF 5.0.x

File

tests/modules/fillpdf_test/fillpdf_test.module
View source
<?php

/**
 * @file
 * Contains fillpdf_test.module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function fillpdf_test_form_fillpdf_settings_alter(array &$form, FormStateInterface $form_state, $form_id) {

  // Add a (redundant) radio for our test backend, not knowing that the backend
  // is autodetected. The label should be overwritten though.
  $form['backend']['#options']['test'] = 'Form-altered pass-through plugin for testing';

  // Add a test setting to the form.
  $form['example_setting'] = [
    '#type' => 'textfield',
    '#title' => 'Form-altered example setting',
    '#default_value' => \Drupal::config('fillpdf.settings')
      ->get('example_setting'),
    '#states' => [
      'visible' => [
        ':input[name="backend"]' => [
          'value' => 'test',
        ],
      ],
    ],
  ];

  // Add custom validate and submit functions.
  $form['#validate'][] = 'fillpdf_test_form_fillpdf_settings_validate';
  $form['#submit'][] = 'fillpdf_test_form_fillpdf_settings_submit';
}

/**
 * Custom validation for fillpdf_test_form_fillpdf_settings_alter().
 */
function fillpdf_test_form_fillpdf_settings_validate($form, FormStateInterface &$form_state) {
  if ($form_state
    ->getValue('backend') == 'test') {

    // Only validate if a value is set.
    $value = $form_state
      ->getValue('example_setting');
    if (!empty($value) && strlen($value) < 2) {
      $form_state
        ->setErrorByName('example_setting', t('Not a valid value.'));
    }
  }
}

/**
 * Handles the ask form submission.
 */
function fillpdf_test_form_fillpdf_settings_submit($form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('backend') == 'test' && ($value = $form_state
    ->getValue('example_setting'))) {

    /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
    $config_factory = \Drupal::service('config.factory');
    $config_factory
      ->getEditable('fillpdf.settings')
      ->set('example_setting', $value)
      ->save();
  }
}

/**
 * Implements hook_entity_operation().
 */
function fillpdf_test_entity_operation(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() == 'fillpdf_form') {
    return [
      'test' => [
        'title' => t('Export configuration test'),
        'url' => $entity
          ->toUrl('export-form'),
      ],
    ];
  }
}

/**
 * Implements hook_entity_operation_alter().
 */
function fillpdf_test_entity_operation_alter(array &$operations, EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() == 'fillpdf_form') {
    $operations['import']['title'] = t('Import configuration test');
  }
}