You are here

class WorkflowTypeForm in Workflow 8

Provides the base form for workflow add and edit forms.

Hierarchy

Expanded class hierarchy of WorkflowTypeForm

File

src/Form/WorkflowTypeForm.php, line 11

Namespace

Drupal\workflow\Form
View source
class WorkflowTypeForm extends EntityForm {

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

    /** @var \Drupal\workflow\Entity\Workflow $workflow */
    $workflow = $this->entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#description' => $this
        ->t('The human-readable label of the workflow.This will be used as a label
         when the workflow status is shown during editing of content.'),
      '#title' => $this
        ->t('Label'),
      '#default_value' => $this->entity
        ->label(),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#description' => $this
        ->t('A unique machine-readable name. Can only contain lowercase letters,
         numbers, and underscores.'),
      '#disabled' => !$this->entity
        ->isNew(),
      '#default_value' => $this->entity
        ->id(),
      '#machine_name' => [
        'exists' => [
          $this,
          'exists',
        ],
        'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
        'error' => $this
          ->t('The machine-readable name must be unique, and can only contain
           lowercase letters, numbers, and underscores.
           Additionally, it can not be the reserved word "custom".'),
      ],
    ];
    $form['permissions'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Workflow permissions'),
      '#open' => TRUE,
      // Controls HTML5 'open' attribute. Defaults to FALSE.
      '#description' => $this
        ->t('To enable further Workflow functionality, go to the
         /admin/people/permissions page and select any roles that should
         have access to below and other functionality.'),
    ];
    $form['permissions']['transition_execute'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Participate in workflow (create, execute transitions)'),
      '#markup' => $this
        ->t("You can determine which roles are enabled on the\n         'Workflow Transitions & roles' configuration page. Use this to enable\n         only the relevant roles. Some sites have too many roles to show on\n         the configuration page."),
    ];
    $form['permissions']['transition_schedule'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Schedule state transition'),
      '#markup' => $this
        ->t('Workflow transitions may be scheduled to a moment
         in the future. Soon after the desired moment, the transition is
         executed by Cron. This may be hidden by settings in widgets,
         formatters or permissions.'),
    ];
    $form['permissions']['history_tab'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Access Workflow history tab'),
      '#markup' => $this
        ->t("You can determine if a tab 'Workflow history' is shown on the entity\n         view page, which gives access to the History of the workflow.\n         If you have multiple workflows per bundle, better disable this\n         feature, and use, clone & adapt the Views display\n         'Workflow history per Entity'."),
    ];
    $form['basic'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Workflow form settings'),
      '#open' => TRUE,
    ];
    $form['basic']['fieldset'] = [
      '#type' => 'select',
      '#options' => [
        0 => $this
          ->t('No fieldset'),
        1 => $this
          ->t('Collapsible fieldset'),
        2 => $this
          ->t('Collapsed fieldset'),
      ],
      '#title' => $this
        ->t('Show the form in a fieldset?'),
      '#default_value' => $workflow
        ->getSetting('fieldset'),
    ];
    $form['basic']['options'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('How to show the available states'),
      '#required' => FALSE,
      '#default_value' => $workflow
        ->getSetting('options'),
      // '#multiple' => TRUE / FALSE,
      '#options' => [
        // These options are taken from options.module.
        'select' => $this
          ->t('Select list'),
        'radios' => $this
          ->t('Radio buttons'),
        'buttons' => $this
          ->t('Action buttons'),
        'dropbutton' => $this
          ->t('Drop button'),
      ],
      '#description' => $this
        ->t('The Widget shows all available states.
         Decide which is the best way to show them.'),
    ];
    $form['basic']['name_as_title'] = [
      '#type' => 'checkbox',
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
      '#title' => $this
        ->t('Use the workflow name as the title of the workflow form'),
      '#default_value' => $workflow
        ->getSetting('name_as_title'),
      '#description' => $this
        ->t('The workflow section of the editing form is in its own fieldset.
         Checking the box will add the workflow name as the title of workflow
         section of the editing form.'),
    ];
    $form['basic']['schedule_enable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable scheduling in Workflow Transition form'),
      '#required' => FALSE,
      '#default_value' => $workflow
        ->getSetting('schedule_enable'),
      '#description' => $this
        ->t('Scheduling may be enabled per Role on /admin/people/permissions page,
        but only if it is enable here.'),
    ];
    $form['basic']['schedule_timezone'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show a timezone when scheduling a transition'),
      '#required' => FALSE,
      '#default_value' => $workflow
        ->getSetting('schedule_timezone'),
    ];

    // @todo D9: remove this, and set default to 1.
    $form['basic']['always_update_entity'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Always update the entity last updated timestamp'),
      '#description' => $this
        ->t('Entity last updated timestamp is always
        updated as long as transition is allowed. This setting is useful to
        indicate that the entity is updated even when transition sid remains
        the same.'),
      '#required' => FALSE,
      '#default_value' => $workflow
        ->getSetting('always_update_entity'),
    ];
    $form['basic']['comment_log_node'] = [
      '#type' => 'select',
      '#required' => FALSE,
      '#options' => [
        // Use 0/1/2 to stay compatible with previous checkbox.
        0 => $this
          ->t('hidden'),
        1 => $this
          ->t('optional'),
        2 => $this
          ->t('required'),
      ],
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
      '#title' => $this
        ->t('How to show the Comment sub-field'),
      '#default_value' => $workflow
        ->getSetting('comment_log_node'),
      '#description' => $this
        ->t('A Comment area can be shown on the Workflow Transition form so that
         the person making a state change can record reasons for doing so.
         The comment is then included in the content\'s workflow history.'),
    ];
    $form['watchdog'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Watchdog'),
      '#description' => $this
        ->t('Informational watchdog messages can be logged when a transition is
         executed (state of a node is changed).'),
      '#open' => TRUE,
    ];
    $form['watchdog']['watchdog_log'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Log watchdog messages upon state change'),
      '#default_value' => $workflow
        ->getSetting('watchdog_log'),
      '#description' => '',
    ];
    return parent::form($form, $form_state);
  }

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

    // $actions['submit']['#value'] = $this->t('Save');
    return $actions;
  }

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

    /** @var \Drupal\workflow\Entity\Workflow $entity */
    $entity = $this->entity;

    // Prevent leading and trailing spaces.
    $entity
      ->set('label', trim($entity
      ->label()));
    $entity
      ->set('options', [
      'name_as_title' => $form_state
        ->getValue('name_as_title'),
      'fieldset' => $form_state
        ->getValue('fieldset'),
      'options' => $form_state
        ->getValue('options'),
      'schedule_enable' => $form_state
        ->getValue('schedule_enable'),
      'schedule_timezone' => $form_state
        ->getValue('schedule_timezone'),
      'always_update_entity' => $form_state
        ->getValue('always_update_entity'),
      'comment_log_node' => $form_state
        ->getValue('comment_log_node'),
      'watchdog_log' => $form_state
        ->getValue('watchdog_log'),
    ]);
    $status = parent::save($form, $form_state);
    $action = $status == SAVED_UPDATED ? 'updated' : 'added';

    // Tell the user we've updated the data.
    $args = [
      '%label' => $entity
        ->label(),
      '%action' => $action,
      'link' => $entity
        ->toLink($this
        ->t('Edit'))
        ->toString(),
    ];
    $this
      ->messenger()
      ->addStatus($this
      ->t('Workflow %label has been %action. Please maintain the permissions,
       states and transitions.', $args));
    $this
      ->logger('workflow')
      ->notice('Workflow %label has been %action.', $args);
    if ($status == SAVED_NEW) {
      $form_state
        ->setRedirect('entity.workflow_type.edit_form', [
        'workflow_type' => $entity
          ->id(),
      ]);
    }
  }

  /**
   * Form validation handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $workflow = $this->entity;
    $name = $workflow
      ->id();

    // Make sure workflow name is not numeric.
    // @todo D8: this was a prerequisite in D7. Remove in D8?
    if (ctype_digit($name)) {
      $form_state
        ->setErrorByName('id', $this
        ->t('Please choose a non-numeric name for your workflow.'));
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * Machine name exists callback.
   *
   * @param string $id
   *   The machine name ID.
   *
   * @return bool
   *   TRUE if an entity with the same name already exists, FALSE otherwise.
   */
  public function exists($id) {
    $type = $this->entity
      ->getEntityTypeId();
    return (bool) $this->entityTypeManager
      ->getStorage($type)
      ->load($id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WorkflowTypeForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
WorkflowTypeForm::exists public function Machine name exists callback.
WorkflowTypeForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
WorkflowTypeForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
WorkflowTypeForm::validateForm public function Form validation handler. Overrides FormBase::validateForm