0

My webshop has lot of users with role Seller. They can add SKU and publish goods. Product display content type has field "Firm" that is taxonomy term reference.

Now every Seller can see all the firms (all the terms) when he add a product and he can check anyone. This is very uncomfortable because Seller work with only a few firms and he want to see only his firms to check one of it.

I want to display only user referenced firms in this field when user add a product.

How do I do this?

  1. I've added to user profile field "Seller Firms" that is the term reference to the Firms. Using module Field Permissions I've made this field accessible to edit only by administrator.

  2. I've checked for each user his firms that he can have access to publish goods of these firms (seller will can to check only those firms that I've checked in profile)

  3. So how to alter product addition form to show only user allowed firms? I have few product displays machine names: product, product_with_attributes

I think this will be hook_alter_form or Rule that will limit the selection to only a few firms that are allowed.

I've made the first part this way: 1. I add Entity Reference field "Seller" to taxonomy Firms that link to users profiles. 2. I'd fill this field for all terms with users that must have access 3. I start to write my module that will display only user referenced terms in node add form. I do not know how to write this. The field name in node Product Display: field_firm. Field Entity Reference in terms: field_seller. Help me to finish my module:

function user_referenced_taxonomy_terms_form_node_form_alter(&$form, &$form_state, $form_id) {
  //$form ['field_firm']
}
  • I've made the first part this way: – tlito Apr 18 '15 at 16:29
0

It is possible using hook_alter_form but hook_form_FORM_ID_alter would be better so you only target the correct form.

You will need code to retrieve only the firms the user has from their profile field and then using array_diff to remove the others from the select options.

| improve this answer | |
  • thank you. how can I get specific info in one place to work with all need functions, variables and get the code I need? api.drupal.org is too difficult to read for me. – tlito Apr 15 '15 at 11:11
  • I doubt whether their is one place as you have a pretty specific use case. You should be able to access the users' profile by calling global $user; – Colin Shipton Apr 15 '15 at 11:14
0

I've made this code without forming new array of allowed firms. Finish it please.

function user_referenced_taxonomy_terms_form_node_form_alter(&$form, &$form_state, $form_id) {

  global $user;
  //if current user has roles administrator or editor return
  foreach ($user->roles as $role) {
      if (($role == 'administrator') || ($role == 'editor')) return;
  }

  $query = new EntityFieldQuery();
  $term_entities = $query
    ->entityCondition('bundle', 'firms')
    ->entityCondition('entity_type', 'taxonomy_term', '=')
    ->fieldCondition('field_seller','target_id',$user->uid) // if user has access
    ->execute();
  if ($term_entities) {
      //forming array of firms using $term_entities array
      //HOW?
      //$form['field_firm']['und'] = drupal_map_assoc( $term_entities );//not correct
  }else {
      //hide field firm if user has no access to any term
      $form['field_firm']['#access'] = FALSE;
  }

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.