2

EDIT: See the original post below for context. I think I've figured out how to target the field's widget, and to do this based on the admin role, but the module doesn't seem to be recognizing the widget type for some reason. The reason I say this is that when the module is enabled, the targeted field disappears in the node edit form. I figure this means it's getting targeted correctly, but can't define how to display itself. I think this might be because the autocomplete widget is defined by the Entity Reference module itself, and not by Core. Do I have to tell the module where to find this widget type? Or is there another reason that this might be happening?

<?php

//Assign role that will be affected by the module
if(user_access('administer')) {
  function entityreference_widget_field_widget_properties_alter(&$widget, $context) {
    // Change entityreference widget 
    $field = $context['field'];
    if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_review_band') {
      $widget['type'] = 'entityreference_autocomplete';
    }
  }
}

ORIGINAL POST: I have an entity reference field that I'm using in a "Review" content type. When users create a "Review", they are given options for this field in a select list based on Bands (another content type) that they have created. If a submitted review needs to be moderated, then an admin must go in and edit the "Review". When the review is edited by an admin, I need the band field to stay the same as it was when originally submitted. However, because of the nature of the how the options for the field are populated, the only options that appear when editing as an admin are "Bands" created by the admin user.

There is a second widget type that can be chosen for the entity reference field, which is autocomplete. Is there a way that I can change the widget type from "select list" to "autocomplete" only when the content is being edited by an administrator? This way, users still have the select list for their bands, and administrators are not limited to only their bands. Otherwise, is there a way to that I can leave this specific field unaltered when editing the content?

2

I ended up getting this to work after finding hook_field_widget_properties_alter. This allows you to, as the hook implies, target the various properties of the widget directly. Global $user seems to provide the $user variable, which allows you to select the role that you want to apply the module to. You can change it from 'administrator' to 'authenticated user', 'anonymous user', or any other needed role to fit your needs.

<?php
function entityreference_widget_field_widget_properties_alter(&$widget, $context) {
  //Assign role that will be affected by the module
  global $user;

  if (in_array('administrator', $user->roles)) { 
    // Change entityreference widget 
    $field = $context['field'];
    if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_review_band') {
      $widget['type'] = 'entityreference_autocomplete';
      $widget['module'] = 'entityreference';
      $widget['settings']['match_operator'] = 'CONTAINS';
      $widget['settings']['size'] = '60';
    }
  }
}

To find out exactly which pieces of the widget needed to be altered, and what to, I created another test field of the type that I wanted to alter my current field to. I then inserted dsm($widget) within the hooks curly brackets of the hook, and then inspected the page with the field on it.

Explanation of dsm() and dpm() for anybody who needs it:

There's so little help anywhere on how to use dsm() or dpm(), and working through this module finally made me understand it. Basically, modules target different elements, and in order to figure out what to target, or what you will be altering, you need to inspect the elements. What dsm() lets you do is view the properties of any given variable (e.g. $form_id most commonly it seems, or in this case $widget or $context) in each of the elements on the page that include it. If anybody stumbles on this and still needs clarification, feel free to ask. It took me forever to figure out how the heck to use dsm().

0

Create

function MODULE_form_CONTENT_TYPE_node_form_alter(&$form, &$form_state, $form_id) { 
  /*code goes here */
}

And change widget type in $form['field_name'] depending of current user roles.

  • Thanks for the suggestions, but would you mind writing out (at least some) of the "code goes here"? I'm still really new when it comes to writing modules/targeting elements, and I'm having problems figuring out how to target the widget type. – Mrweiner Jul 23 '12 at 18:19
0

Here is another example I was able to implement thanks to Mrweiner's post.

I wanted to change the widget from a normal autocomplete, to an autocomplete with node creation, depending upon a special user permission.

I am using the references module and the node reference create module. The site it is from is from an early Drupal 7 install before entity reference was available.

function YOURMODULENAME_field_widget_properties_alter(&$widget, $context) {

 if (user_access('EXAMPLE PERMISSION NAME')) {

    $field = $context['field'];

     if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_example_ref') {

      $widget['type'] = 'noderefcreate_autocomplete';
      $widget['module'] = 'noderefcreate';
      $widget['settings']['autocomplete_match'] = 'starts_with';
      $widget['settings']['size'] = '60';
      $widget['settings']['autocomplete_path'] = 'node_reference/autocomplete';
    }
  }
}

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.