Exemple #1
0
 /**
  * Implements hook_field_settings_form() -> ConfigFieldItemInterface::settingsForm().
  */
 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.
     $workflows = array();
     $workflows[''] = t('- Select a value -');
     foreach (workflow_load_multiple() as $wid => $workflow) {
         if ($workflow->isValid()) {
             $workflows[$wid] = check_plain($workflow->label());
             // No t() on settings page.
         }
     }
     // 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.'));
     $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;
 }
Exemple #2
0
 /**
  * Given information, update or insert a new workflow.
  *
  * This also handles importing, rebuilding, reverting from Features,
  * as defined in workflow.features.inc.
  * todo: reverting does not refresh States and transitions, since no
  * machine_name was present. As of 7.x-2.3, the machine_name exists in
  * Workflow and WorkflowConfigTransition, so rebuilding is possible.
  *
  * When changing this function, test with the following situations:
  * - maintain Workflow in Admin UI;
  * - clone Workflow in Admin UI;
  * - create/revert/rebuild Workflow with Features; @see workflow.features.inc
  * - save Workflow programmatically;
  */
 public function save($create_creation_state = TRUE)
 {
     // Are we saving a new Workflow?
     $is_new = !empty($this->is_new);
     // Are we rebuilding, reverting a new Workflow? @see workflow.features.inc
     $is_rebuild = !empty($this->is_rebuild);
     $is_reverted = !empty($this->is_reverted);
     // If rebuild by Features, make some conversions.
     if (!$is_rebuild && !$is_reverted) {
         // Avoid troubles with features clone/revert/..
         unset($this->module);
     } else {
         $role_map = isset($this->system_roles) ? $this->system_roles : array();
         if ($role_map) {
             // Remap roles. They can come from another system with shifted role IDs.
             // See also https://drupal.org/node/1702626 .
             $this->tab_roles = _workflow_rebuild_roles($this->tab_roles, $role_map);
             foreach ($this->transitions as &$transition) {
                 $transition['roles'] = _workflow_rebuild_roles($transition['roles'], $role_map);
             }
         }
         // Insert the type_map when building from Features.
         if ($this->typeMap) {
             foreach ($this->typeMap as $node_type) {
                 workflow_insert_workflow_type_map($node_type, $this->wid);
             }
         }
     }
     // After update.php or import feature, label might be empty. @todo: remove in D8.
     if (empty($this->label)) {
         $this->label = $this->name;
     }
     $return = parent::save();
     // If a workflow is cloned in Admin UI, it contains data from original workflow.
     // Redetermine the keys.
     if ($is_new && $this->states) {
         foreach ($this->states as $state) {
             // Can be array when cloning or with features.
             $state = is_array($state) ? new WorkflowState($state) : $state;
             // Set up a conversion table, while saving the states.
             $old_sid = $state->sid;
             $state->wid = $this->wid;
             // @todo: setting sid to FALSE should be done by entity_ui_clone_entity().
             $state->sid = FALSE;
             $state->save();
             $sid_conversion[$old_sid] = $state->sid;
         }
         // Reset state cache.
         $this->getStates(TRUE, TRUE);
         foreach ($this->transitions as &$transition) {
             // Can be array when cloning or with features.
             $transition = is_array($transition) ? new WorkflowConfigTransition($transition, 'WorkflowConfigTransition') : $transition;
             // Convert the old sids of each transitions before saving.
             // @todo: is this be done in 'clone $transition'?
             // (That requires a list of transitions without tid and a wid-less conversion table.)
             if (isset($sid_conversion[$transition->sid])) {
                 $transition->tid = FALSE;
                 $transition->sid = $sid_conversion[$transition->sid];
                 $transition->target_sid = $sid_conversion[$transition->target_sid];
                 $transition->save();
             }
         }
     }
     // Make sure a Creation state exists.
     if ($is_new) {
         $state = $this->getCreationState();
     }
     // Make sure the default roles are permitted in transitions for better UX.
     if ($is_new && count(workflow_load_multiple()) == 1) {
         foreach (user_roles() as $rid => $name) {
             $perms = array('participate in workflow' => 1);
             user_role_change_permissions($rid, $perms);
             // <=== Enable Roles
         }
     }
     workflow_reset_cache($this->wid);
     return $return;
 }