2

I have a workflow system for my nodes, where one of the states is Disabled. There are some flags on those nodes as well; I'd like to to revoke access to flags to the node author when the workflow state is changed to Disabled.

I'm looking for any solution, contributed modules or custom modules, or just a combination of modules.

I'm not sure if hook_flag_access() is relevant here or not.

1
  • I’m voting to close this question because it's asking for recommendations or suggestions on hosting, tools, modules, themes, distributions, books, tutorials, documentation, or other off-site resources.
    – avpaderno
    Commented Apr 10, 2022 at 16:34

2 Answers 2

2
+50

If you need custom module, you can try this code

/**
 * Check status of node
 * @param $nid
 * @param $fid
 * @return $result
 * if node have status disable return array()
 * if node have status active return NUll
 */
function check_workflow_status($nid, $fid){
  $result = db_select('flagging', 'fg')
    ->fields('fg', array('entity_id'))
    ->condition('entity_id', $nid, '=')
    ->condition('fid', $fid, '=')
    ->execute()
    ->fetchAssoc();
  return $result;
}

/**
 * Implement hook_flag_access()
 *
 */
function YOURMODULE_flag_access($flag, $entity_id, $action, $account) {
  // Change flag id with your flag's ID
  // You can dpm($flag) to see flag id of status node.
  $result = check_workflow_status($entity_id, 1);
  if (!empty($result)) {
    // You need check special use have permission use flag status
    if (user_access('flag node_status'))
      return TRUE;
    else
      // Deny access to all other flags of the node. 
      return FALSE;

  }
}

You can get string in user_access() at here.

0
1

hook_flag_access and hook_flag_multiple_access() do seem to be the way to go. You will need to write a small custom module for that, which you can read more from here.

An example implementation is below:

<?php

function MYMODULE_flag_access($flag, $entity_id, $action, $account) {
  if ($flag->name == 'NAME' && $flag->entity_type == 'node') {// Change flag name with your flag's machine name.
    $node = node_load($entity_id);
    if ($node && $node->uid == $account->uid) {
      return FALSE;
    }
  }

  return NULL;
}

function MYMODULE_flag_access_multiple($flag, $entity_ids, $account) {
  if ($flag->name == 'NAME' && $flag->entity_type == 'node') { // Change flag name with your flag's machine name.
    $nodes = node_load_multiple($entity_ids);
    foreach ($nodes as &$node) {
      $node = $node->uid == $account->uid ? FALSE : NULL;
    }
    return $nodes;
  }
  return array();
}

You will need to change "MYMODULE" with a module name of your choice, and use the same name and save it as MYMODULE.module. Accompany with the MYMODULE.info, drop in a folder and activate the module. Above is a sample code for you to start with the module. It checks if the Flag's entity type is node. If it's node, the relevant node(s) will be loaded, and the user ID will be matched against the provided account's user ID. If they match, it returns FALSE, indicating access should be denied. It does not explicitly return TRUE, to allow the basic Flag access checks and potential other modules to determine the access.

0