how to limit the column or fields to only a sepecafic role in views Drupal? - drupal-views

I have created a view of basic pages content type in drupal and I want to make visible the title field in a view to only admin user and all other fields or column should be visible to all other users including admin. please any one have idea about this?

Issue is solved now,
solution: create a php field and add following snippet.
roles)) {
print '$'. $data->_field_data['nid']['entity']->field_name_to_display['und'][0]['value'];
}else{
print "no access";
}
?>

Related

Populate a Dynamic Field Dropdown List in OTRS

I need to add a dropdown in the New Ticket Screen of OTRS. I managed to add a Dropdown by adding a Dynamic Field with the help of Dynamic Fields Management in Admin Section.
Now my problem is that I want to populate this Dropdown with data that I get from some distant database on the run and dependin on the User Loged In. How can i feed In this Dynamic Data in the DropDown List in OTRS ?
Thank you.
To do such a thing I do not believe is supported from the Dynamic Field UI provided by OTRS.
So you can either:
1- add all the possible values into the drop down box and then hide/show them using code changes in the dtl file. (use javascript).
For creating a new ticket there is either AgentTicketEmail.dtl or AgentTicketPhone.dtl.
There is also the CustomerTicketMessage.dtl if you want to include it in the customer interface too.
2- Add only one value which you can also hide using javascript in the dtl files and just add values to the dropdown list using javascript code.
Example javascript below hides/shows different dynamic fields. You can find what your dynamic field is called by looking at the page source from your browser.
function setdynamicviews(){
switch ($('#Dest').val() ) { //this is where the queue is relevant (Dest = Queue)
case "8\|\|Support": // need to slash escape the pipes
//show dynamic fields
document.getElementById('LabelDynamicField_Product').style.display = 'block';
document.getElementById('LabelDynamicField_SerialNo').style.display = 'block';
break;
default:
//hide dynamic fields.
document.getElementById('LabelDynamicField_Product').style.display = 'none';
document.getElementById('LabelDynamicField_SerialNo').style.display = 'none';
}
}
To add items to usign javascript see here
Yuu have not provided enough information for me to help with getting the information "from some distant database"
Note: if you do change any DTL files or other otrs files you should defrinitely create a theme first see here
Hope this helps.

Unique field in user registration form in Drupal 7

I am working in drupal 7. I need a field in the user registration form which needs to be unique. It is an integer field which stores the ID of the user. I have been searching around for a few hours now without any development. Can someone guide me through this?
Thank You.
You can add a custom "staff id" field to the user entity type from admin/config/people/account/fields (configuration->people->account settings). You can add a new integer field, and mark it to display in the registration form, and/or required.
To check that the field value is unqiue you will need to use a custom module. In your custom module use the form_id_form_alter hook to add a custom validation to the registration form. Then during validation you can check that the value does not already exist in the database and return a form error.
Example of the custom module:
<?php
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id){
$form['#validate'][] = 'mymodule_user_registration_validate';
}
function mymodule_user_registration_validate(&$form,&$form_state){
$staff_id = $form_state['values']['staff_id_field'];
$check_unique_query = 'SELECT field_staff_id_value FROM {field_data_staff_id} WHERE field_staff_id_value = :staff_id LIMIT 1';
//if a result is returned, that means the $staff_id exists
if (db_query($check_unique_query,array(':staff_id'=>$staff_id))->fetchField()){
form_set_error('staff_id_field','This Staff ID is already in use');
}
}
Have you tried the Unique field module? Pretty sure it does what you need here.
Field validation does the trick https://drupal.org/project/field_validation. You can set any rules per field even on the registration form

Hide image field completely in drupal 7 module

I'm trying to setup a form where some fields needs to be hidden depending on the user role. I'm doing this in my own module using hook_form_FORM_ID_alter. No problem with common text, email or link fields (e.g. $form['field_companyname']['und'][0]['value']['#type']='hidden'). But for an image field or a multiple value file field the usual way won't work.
Anybody can give me a clue?
I think your going about this the wrong way. Since your limitation is based on roles you can just use the permissions system. Check out field permissions module.
I recommend you to use #access for the element instead of just hiding the field.
For field company name it will go like this:
$form['field_companyname']['#access'] = FALSE;

Joomla Form Field User Front End

I am trying to use JFormFieldUser::getInput as an input to my Joomla forms.
In the backend (logged in with the super user), when I call this method, it produces a nice 'select user' box when clicked displays a list of all users to chose from.
I have been trying to use the User form field on a front end form (logged in with the super user). The result is some what confusing and undesirable. A 'select user' link is produced, but when clicking on it, the result is that the super users, 'User profile' is loaded up: not a list of all users.
Why is this, and how can i make 'select user' show the full list of users like it does in the backend.
Joomla 3 (from your field definitions):
<field name="field_name" type="sql" label="COM_FIELD_LABEL"
sql_select="id,name"
sql_from="#__users"
value_field="name"
key_field="id"
description=""
header="DROP_DOWN_HEADER"
required="false" />
Apparently it can't be done
The JForm (Joomla) User field is the Joomla core field that you can
see in Joomla article form to select a user (lightbox with list of
user). Becareful this field can not be used on front-end because
Joomla core don't manage it on front-end... Often we replace this
field on front end with a select dynamic field.
I didn't have much luck with this. Instead I created my own component and added in the content from com_users. Worked a treat.
Can be done fairly simply but with some significant limitations - depending on your use case.
User must be logged in to backend (even if you are trying to access the information frontend). If they are not logged in, it will prompt for log in and break out of the modal :(
User must have permissions for User Manager
Then duplicate the \libraries\cms\form\field\user.php to a fields location of your choice (in a modal subdirectory) and rename it to something like user2.php. Make the class name JFormFieldModal_Users2 and the $type='Modal_Users2'.
Don't forget to add the new path to your form .xml if required. The type will be "modal_users2".
Last step. In user2.php, change:
$link = 'index.php?option=com_users&view=users&layout=modal&tmpl=component&field=' . $this->id
. (isset($groups) ? ('&groups=' . base64_encode(json_encode($groups))) : '')
. (isset($excluded) ? ('&excluded=' . base64_encode(json_encode($excluded))) : '');
to
$link = 'administrator/index.php?option=com_users&view=users&layout=modal&tmpl=component&field=' . $this->id
. (isset($groups) ? ('&groups=' . base64_encode(json_encode($groups))) : '')
. (isset($excluded) ? ('&excluded=' . base64_encode(json_encode($excluded))) : '');
A bit hacky, but served my purposes.
Less hacky, but less glamorous solution here: The SQL formfield type

Zend hidden elements: hide html values

I am facing a special scenario here on some of my forms.
I have settled a permission system over some fields many of which are required.
When removing the permission to view the field on a form, I set:
$field->setDecorators('disableLoadDefaultDecorators', true);
The problem in that case is that I get prompted with the validation error over the required field, which is logical.
The other option would be to set the $field to hidden but the issue turns to become an html problem where any person can retrieve the hidden value through the source code.
Hopefully someone can offer me a suggestion on how to hide the element from the form and metadata but return it on form validation as if it was displayed.
Thank you in advance!
Change the field validation rules so that it is not required:
$field->setRequired(false)->setDecorators('disableLoadDefaultDecorators', true);