示例#1
0
 /**
  * Implements hook_field_settings_form() -> ConfigFieldItemInterface::settingsForm().
  */
 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data)
 {
     $element = array();
     // Create list of all Workflow types. Include an initial empty value.
     // Validate each workflow, and generate a message if not complete.
     $workflows = workflow_get_workflow_names(FALSE);
     // @todo D8: add this to WorkflowFieldConstraintValidator.
     // Set message, if no 'validated' workflows exist.
     if (count($workflows) == 1) {
         drupal_set_message(t('You must create at least one workflow before content can be
       assigned to a workflow.'), 'warning');
     }
     // Validate via annotation WorkflowFieldConstraint. Show a message for each error.
     $violation_list = $this->validate();
     foreach ($violation_list->getIterator() as $violation) {
         switch ($violation->getPropertyPath()) {
             case 'fieldnameOnComment':
                 // A 'comment' field name MUST be equal to content field name.
                 // @todo: Still not waterproof. You could have a field on a non-relevant entity_type.
                 drupal_set_message($violation->getMessage(), 'error');
                 $workflows = array();
                 break;
             default:
                 break;
         }
     }
     // Set the required workflow_type on 'comment' fields.
     // N.B. the following must BELOW the (count($workflows) == 1) snippet.
     $field_storage = $this->getFieldDefinition()->getFieldStorageDefinition();
     if (!$this->getSetting('workflow_type') && $field_storage->getTargetEntityTypeId() == 'comment') {
         $field_name = $field_storage->get('field_name');
         $workflows = array();
         foreach (_workflow_info_fields($entity = NULL, $entity_type = '', $entity_bundle = '', $field_name) as $key => $info) {
             if ($info->getName() == $field_name && $info->getTargetEntityTypeId() !== 'comment') {
                 $wid = $info->getSetting('workflow_type');
                 $workflow = Workflow::load($wid);
                 $workflows[$wid] = $workflow->label();
             }
         }
     }
     // Let the user choose between the available workflow types.
     $wid = $this->getSetting('workflow_type');
     $url = \Drupal\Core\Url::fromRoute('entity.workflow_type.collection');
     $element['workflow_type'] = array('#type' => 'select', '#title' => t('Workflow type'), '#options' => $workflows, '#default_value' => $wid, '#required' => TRUE, '#disabled' => $has_data, '#description' => t('Choose the Workflow type. Maintain workflows
      <a href=":url">here</a>.', array(':url' => $url->toString())));
     // Get a string representation to show all options.
     /*
      * Overwrite ListItemBase::storageSettingsForm().
      */
     $allowed_values = WorkflowState::loadMultiple([], $wid);
     $allowed_values_function = $this->getSetting('allowed_values_function');
     $element['allowed_values'] = array('#type' => 'textarea', '#title' => t('Allowed values for the selected Workflow type'), '#default_value' => $wid ? $this->allowedValuesString($allowed_values) : [], '#rows' => count($allowed_values), '#access' => $wid ? TRUE : FALSE, '#disabled' => TRUE, '#element_validate' => array(array(get_class($this), 'validateAllowedValues')), '#field_has_data' => $has_data, '#field_name' => $this->getFieldDefinition()->getName(), '#entity_type' => $this->getEntity()->getEntityTypeId(), '#allowed_values' => $allowed_values);
     $element['allowed_values']['#description'] = $this->allowedValuesDescription();
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function buildConfigurationForm(array $form, FormStateInterface $form_state)
 {
     $form = [];
     // If we are on admin/config/system/actions and use CREATE AN ADVANCED ACTION
     // Then $context only contains:
     // - $context['actions_label'] = "Change workflow state of post to new state"
     // - $context['actions_type'] = "entity"
     //
     // If we are on a VBO action form, then $context only contains:
     // - $context['entity_type'] = "node"
     // - $context['view'] = "(Object) view"
     // - $context['settings'] = "array()"
     // @todo: test with multiple workflows per entity.
     $wids = workflow_get_workflow_names();
     $wid = array_keys($wids)[0];
     // Get the common Workflow, or create a dummy Workflow.
     $workflow = $wid ? Workflow::load($wid) : Workflow::create(['id' => 'dummy_action', 'label' => 'dummy_action']);
     $current_state = $workflow->getCreationState();
     /* // @TODO D8-port
         // Show the current state and the Workflow form to allow state changing.
         // N.B. This part is replicated in hook_node_view, workflow_tab_page, workflow_vbo.
         if ($workflow) {
           $field = _workflow_info_field($field_name, $workflow);
           $field_name = $field['field_name'];
           $field_id = $field['id'];
           $instance = field_info_instance($entity_type, $field_name, $entity_bundle);
     
           // Hide the submit button. VBO has its own 'next' button.
           $instance['widget']['settings']['submit_function'] = '';
           if (!$field_id) {
             // This is a Workflow Node workflow. Set widget options as in v7.x-1.2
             $field['settings']['widget']['comment'] = isset($workflow->options['comment_log_tab']) ? $workflow->options['comment_log_tab'] : 1; // vs. ['comment_log_node'];
             $field['settings']['widget']['current_status'] = TRUE;
             // As stated above, the options list is probably very long, so let's use select list.
             $field['settings']['widget']['options'] = 'select';
             // Do not show the default [Update workflow] button on the form.
             $instance['widget']['settings']['submit_function'] = '';
           }
         }
     
         // Add the form/widget to the formatter, and include the nid and field_id in the form id,
         // to allow multiple forms per page (in listings, with hook_forms() ).
         // Ultimately, this is a wrapper for WorkflowDefaultWidget.
         // $form['workflow_current_state'] = workflow_state_formatter($entity_type, $entity, $field, $instance);
         $form_id = implode('_', array(
           'workflow_transition_form',
           $entity_type,
           $entity_id,
           $field_id
         ));
     */
     $transition = $this->getTransitionForConfiguration($current_state);
     // Add the WorkflowTransitionForm to the page.
     // Here, not the $element is added, but the entity form.
     $element = [];
     // Just to be explicit.
     $element['#default_value'] = $transition;
     $form += WorkflowTransitionElement::transitionElement($element, $form_state, $form);
     // Remove the transition: generates an error upon saving the action definition.
     unset($form['workflow_transition']);
     // Todo D8: add the entity form.
     //$form = \Drupal::getContainer()->get('entity.form_builder')->getForm($transition, 'add');
     // Remove the action button. The Entity itself has one.
     //unset($element['actions']);
     // Make adaptations for VBO-form:
     $entity = $transition->getTargetEntity();
     $field_name = $transition->getFieldName();
     $force = $this->configuration['force'];
     // Override the options widget.
     $form['to_sid']['#description'] = t('Please select the state that should be assigned when this action runs.');
     // Add Field_name. @todo?? Add 'field_name' to WorkflowTransitionElement?
     $form['field_name'] = array('#type' => 'select', '#title' => t('Field name'), '#description' => t('Choose the field name.'), '#options' => _workflow_info_field_names($entity), '#default_value' => $field_name, '#required' => TRUE, '#weight' => -20);
     // Add Force. @todo?? Add 'force' to WorkflowTransitionElement?
     $form['force'] = array('#type' => 'checkbox', '#title' => t('Force transition'), '#description' => t('If this box is checked, the new state will be assigned even if workflow permissions disallow it.'), '#default_value' => $force, '#weight' => -19);
     // Change comment field.
     $form['comment'] = array('#title' => t('Message'), '#description' => t('This message will be written into the workflow history log when the action
   runs. You may include the following variables: %state, %title, %user.')) + $form['comment'];
     return $form;
 }
 /**
  * Implements hook_field_settings_form() -> ConfigFieldItemInterface::settingsForm().
  *
  * @param array $form
  * @param array $form_state
  * @param $has_data
  *
  * @return array $element
  *   The newly constructed element.
  */
 public function settingsForm(array $form, array &$form_state, $has_data)
 {
     $field_info = self::getInfo();
     $settings = $this->field['settings'];
     $settings += $field_info['workflow']['settings'];
     $settings['widget'] += $field_info['workflow']['settings']['widget'];
     // Create list of all Workflow types. Include an initial empty value.
     // Validate each workflow, and generate a message if not complete.
     /* @var $workflow Workflow */
     $workflows = array();
     $workflows[''] = t('- Select a value -');
     foreach ($workflows += workflow_get_workflow_names() as $wid => $label) {
         $workflow = workflow_load_single($wid);
         if ($wid && !$workflow->isValid()) {
             unset($workflows[$wid]);
         }
     }
     // Set message, if no 'validated' workflows exist.
     if (count($workflows) == 1) {
         drupal_set_message(t('You must create at least one workflow before content can be
       assigned to a workflow.'));
     }
     // The allowed_values_functions is used in the formatter from list.module.
     $element['allowed_values_function'] = array('#type' => 'value', '#value' => $settings['allowed_values_function']);
     // $field['settings']['wid'] can be numeric or named, or empty.
     $wid = isset($settings['wid']) ? $settings['wid'] : '';
     // Let the user choose between the available workflow types.
     $element['wid'] = array('#type' => 'select', '#title' => t('Workflow type'), '#options' => $workflows, '#default_value' => $wid, '#required' => TRUE, '#disabled' => $has_data, '#description' => t('Choose the Workflow type. Maintain workflows !url.', array('!url' => l(t('here'), 'admin/config/workflow/workflow'))));
     // Inform the user of possible states.
     // If no Workflow type is selected yet, do not show anything.
     if ($wid) {
         // Get a string representation to show all options.
         $allowed_values_string = $this->_allowed_values_string($wid);
         $element['allowed_values_string'] = array('#type' => 'textarea', '#title' => t('Allowed values for the selected Workflow type'), '#default_value' => $allowed_values_string, '#rows' => 10, '#access' => TRUE, '#disabled' => TRUE);
     }
     $element['widget'] = array('#type' => 'fieldset', '#title' => t('Workflow widget'), '#description' => t('Set some global properties of the widgets for this
     workflow. Some can be altered per widget instance.'));
     $fieldset_options = array(0 => t('No fieldset'), 1 => t('Collapsible fieldset'), 2 => t('Collapsed fieldset'));
     $element['widget']['fieldset'] = array('#type' => 'select', '#options' => $fieldset_options, '#title' => t('Show the form in a fieldset?'), '#default_value' => $settings['widget']['fieldset'], '#description' => t("The Widget can be wrapped in a visible fieldset. You'd\n        do this when you use the widget on a Node Edit page."));
     $element['widget']['options'] = array('#type' => 'select', '#title' => t('How to show the available states'), '#required' => FALSE, '#default_value' => $settings['widget']['options'], '#options' => array('select' => 'Select list', 'radios' => 'Radio buttons', 'buttons' => 'Action buttons'), '#description' => t("The Widget shows all available states. Decide which\n        is the best way to show them. ('Action buttons' do not work on Comment form.)"));
     $element['widget']['hide'] = array('#type' => 'checkbox', '#attributes' => array('class' => array('container-inline')), '#title' => t('Hide the widget on Entity form.'), '#default_value' => $settings['widget']['hide'], '#description' => t('Using Workflow Field, the widget is always shown when editing an
     Entity. Set this checkbox in case you only want to change the status
     on the Workflow History tab or on the Node View. (This checkbox is
     only needed because Drupal core does not have a "hidden" widget.)'));
     $element['widget']['name_as_title'] = array('#type' => 'checkbox', '#attributes' => array('class' => array('container-inline')), '#title' => t('Use the workflow name as the title of the workflow form'), '#default_value' => $settings['widget']['name_as_title'], '#description' => 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.'));
     $element['widget']['schedule'] = array('#type' => 'checkbox', '#title' => t('Allow scheduling of workflow transitions.'), '#required' => FALSE, '#default_value' => $settings['widget']['schedule'], '#description' => 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.'));
     $element['widget']['schedule_timezone'] = array('#type' => 'checkbox', '#title' => t('Show a timezone when scheduling a transition.'), '#required' => FALSE, '#default_value' => $settings['widget']['schedule_timezone']);
     $element['widget']['comment'] = array('#type' => 'select', '#title' => t('Allow adding a comment to workflow transitions'), '#required' => FALSE, '#options' => array(0 => t('hidden'), 1 => t('optional'), 2 => t('required')), '#default_value' => $settings['widget']['comment'], '#description' => t('On the Workflow form, a Comment form can be included
     so that the person making the state change can record reasons for doing
     so. The comment is then included in the node\'s workflow history. This
     may be altered by settings in widgets, formatters or permissions.'));
     $element['watchdog_log'] = array('#type' => 'checkbox', '#attributes' => array('class' => array('container-inline')), '#title' => t('Log informational watchdog messages when a transition is
     executed (a state value is changed)'), '#default_value' => $settings['watchdog_log'], '#description' => t('Optionally log transition state changes to watchdog.'));
     $element['history'] = array('#type' => 'fieldset', '#title' => t('Workflow history'), '#collapsible' => TRUE, '#collapsed' => FALSE);
     $element['history']['history_tab_show'] = array('#type' => 'checkbox', '#title' => t('Use the workflow history, and show it on a separate tab.'), '#required' => FALSE, '#default_value' => $settings['history']['history_tab_show'], '#description' => t("Every state change is recorded in table\n        {workflow_node_history}. If checked and user has proper permission, a\n        tab 'Workflow' is shown on the entity view page, which gives access to\n        the History of the workflow. If you have multiple workflows per bundle,\n        better disable this feature, and use, clone & adapt the Views display\n        'Workflow history per Entity'."));
     $element['history']['roles'] = array('#type' => 'checkboxes', '#options' => workflow_get_roles(), '#title' => t('Workflow history permissions'), '#default_value' => $settings['history']['roles'], '#description' => t('Select any roles that should have access to the workflow tab on nodes that have a workflow.'));
     return $element;
 }