You are here

function workflow_access_features_rebuild in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow_access/workflow_access.features.inc \workflow_access_features_rebuild()

Implements hook_features_rebuild().

Instruct Features to insert our records (that were exported into code) into the workflow_access table.

1 call to workflow_access_features_rebuild()
workflow_access_features_revert in workflow_access/workflow_access.features.inc
Implements hook_features_revert().

File

workflow_access/workflow_access.features.inc, line 127
Workflow_access records are a **faux-exportable** component.

Code

function workflow_access_features_rebuild($module) {

  // @see d.o. https://www.drupal.org/node/2472501
  //  $workflow_access_records = module_invoke($module, 'workflow_access_features_default_settings');
  if (!($workflow_access_records = features_get_default('workflow_access', $module))) {
    return;
  }

  // Retrieve the workflow IDs.
  $wids = array();
  foreach ($workflow_access_records as $workflow_name => $state) {
    $workflow = workflow_load_by_name($workflow_name);
    $wids[$workflow_name] = $workflow->wid;
  }
  foreach ($wids as $workflow_name => $wid) {
    $workflow = workflow_load_by_name($workflow_name);
    if (!empty($workflow) && ($states = $workflow
      ->getStates($all = TRUE))) {
      foreach ($states as $state) {

        // Remove all workflow access records for states belonging to this
        // workflow. We don't want lingering entries - we only want the ones we're
        // about to insert.
        workflow_access_delete_workflow_access_by_sid($state->sid);
      }
    }
  }

  // Insert our workflow access records. They look like
  // workflow_name[state_name][role_name] => array(grant_name => 0|1, ...)
  foreach ($workflow_access_records as $workflow_name => $states) {
    foreach ($states as $state_name => $records) {
      if ($state = workflow_state_load_by_name($state_name, $wids[$workflow_name])) {
        foreach ($records as $rname => $record) {
          $record['sid'] = $state ? $state->sid : $state->sid;
          if ($rname == WORKFLOW_FEATURES_AUTHOR_NAME) {
            $record['rid'] = WORKFLOW_ROLE_AUTHOR_RID;
          }
          else {
            $role = user_role_load_by_name($rname);
            $record['rid'] = $role->rid;
          }
          workflow_access_insert_workflow_access_by_sid($record);
        }
      }
    }
  }
}