You are here

class ChangeFormDisplay in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/ChangeFormDisplay.php \Drupal\business_rules\Plugin\BusinessRulesAction\ChangeFormDisplay

Class ChangeFormDisplay.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "change_form_display",
  label = @Translation("Change form display"),
  group = @Translation("Entity"),
  description = @Translation("Display form using a custom form mode."),
  isContextDependent = TRUE,
  hasTargetEntity = TRUE,
  hasTargetBundle = FALSE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of ChangeFormDisplay

File

src/Plugin/BusinessRulesAction/ChangeFormDisplay.php, line 27

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction
View source
class ChangeFormDisplay extends BusinessRulesActionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    if ($item
      ->isNew()) {
      return [];
    }
    $form_modes = \Drupal::service('entity_display.repository')
      ->getFormModes($item
      ->getTargetEntityType());
    $options = [];
    foreach ($form_modes as $key => $value) {
      $options[$key] = $value['label'];
    }
    uasort($options, function ($a, $b) {
      return $a['label'] < $b['label'] ? -1 : 1;
    });
    $settings['selection_mode'] = [
      '#type' => 'radios',
      '#title' => t('Selection mode'),
      '#description' => t('How do you want to set the form display mode?'),
      '#default_value' => $item
        ->getSettings('selection_mode'),
      '#required' => TRUE,
      '#options' => [
        'fixed' => t('Fixed'),
        'variable' => t('Using variables or token'),
      ],
    ];
    $settings['fixed'] = [
      '#type' => 'select',
      '#title' => t('Form Mode'),
      '#default_value' => $item
        ->getSettings('fixed'),
      '#options' => $options,
      '#empty_option' => t('- Select -'),
      '#states' => [
        'visible' => [
          ':input[name="selection_mode"]' => [
            'value' => 'fixed',
          ],
        ],
      ],
    ];
    $settings['variable'] = [
      '#type' => 'textfield',
      '#title' => t('Variable or token'),
      '#description' => t('The variable/token must hold the value of the view mode id without the entity type.'),
      '#default_value' => $item
        ->getSettings('variable'),
      '#states' => [
        'visible' => [
          ':input[name="selection_mode"]' => [
            'value' => 'variable',
          ],
        ],
      ],
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\business_rules\ItemInterface $item */
    $item = $form_state
      ->get('business_rules_item');

    // We only can validate the form if the item is not new.
    if (!empty($item) && !$item
      ->isNew()) {
      if ($form_state
        ->getValue('selection_mode') == 'fixed') {
        if (!$form_state
          ->getValue('fixed')) {
          $form_state
            ->setErrorByName('fixed', t('Select the form mode'));
        }
      }
      elseif (!$form_state
        ->getValue('variable')) {
        $form_state
          ->setErrorByName('variable', t('Fill the variable/token'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function execute(ActionInterface $action, BusinessRulesEvent $event) {
    $variables = $event
      ->getArgument('variables');
    $selection_mode = $action
      ->getSettings('selection_mode');
    $fixed = $action
      ->getSettings('fixed');
    $variable = $action
      ->getSettings('variable');
    $variable = $this
      ->processVariables($variable, $variables);

    // Remove the first part of the machine name id it's on the variable value.
    $arr = explode('.', $variable);
    $variable = isset($arr[1]) ? $arr[1] : $arr[0];
    $entity_type = $event
      ->getArgument('entity_type_id');
    $bundle = $event
      ->getArgument('bundle');
    $form_display_mode = $selection_mode == 'fixed' ? $fixed : $variable;
    $form_display = \Drupal::entityTypeManager()
      ->getStorage('entity_form_display')
      ->load($entity_type . '.' . $bundle . '.' . $form_display_mode);
    $event
      ->setArgument('form_display', $form_display);
    $result = [
      '#type' => 'markup',
      '#markup' => t('Form display changed to: %form_display, on entity type: %entity_type, bundle: %bundle.', [
        '%form_display' => $form_display_mode,
        '%entity_type' => $entity_type,
        '%bundle' => $bundle,
      ]),
    ];
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface::getVariables 9
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginInterface::processSettings 19
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
ChangeFormDisplay::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
ChangeFormDisplay::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
ChangeFormDisplay::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginBase::validateForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.