Drupal Organic Groups: Entity Reference Other groups - drupal-7

I have Organic Groups installed on my Drupal website. I created groups and a group content type. In the group content type I have an OG Reference field to refer to the group it can belong. However, this field also automatically shows a "Other groups" field. How can I remove this "Other groups" field, as I do not want to enable users to choose other groups to fill in.
Thanks!

You can hide it by using hook_field_widget_form_alter():
/**
* Implements hook_field_widget_form_alter().
*/
function fr_groups_field_widget_form_alter(&$element, &$form_state, $context) {
// Hide "Other groups" table for group selection.
if ($element['#field_name'] == 'og_group_ref' && isset($element['admin'])) {
$element['admin']['#access'] = FALSE;
}
}

The "Other groups" field is only visible to users with "Administer Group" permissions. This permission is under:
/drupal/admin/config/group/permissions/node/%node%
I wouldn't worry about this since regular users won't see this but if you want this removed you can remove this field for ALL users by removing ALL roles from Administer Group permissions. Just unselect the option in the above mentioned URL.

Related

How to retrieve Images of a User being part of a Group with Collection Permissions in Wagtail?

I have a wagtail installation using the Multisite pattern, where I have a group of user per site and each group as it's own collection.
When the User logged in the admin interface, they see in the Summary section the image count from all the collections.
But when they click the image menu, they only see the images within their group collection. I found it confusing that they could know the total count of all collections. I wanted to get the count from the collection the user had rights for.
I figured out I could override the ImagesSummaryItem and I ended up coding the following snippet of code:
class CorrectedImagesSummaryItem(SummaryItem):
order = 200
template = 'wagtailimages/homepage/site_summary_images.html'
def get_context(self):
site_name = get_site_for_user(self.request.user)['site_name']
permissions = Permission.objects.filter(
content_type=ContentType.objects.get_for_model(get_image_model()),
codename__in=['change_image', 'add_image'])
collections = Collection.objects.filter(
group_permissions__group__in=self.request.user.groups.all(),
group_permissions__permission__in=permissions
).distinct()
if collections:
image_count = get_image_model().objects.filter(collection__in=collections).count()
else:
image_count = 0
return {
'total_images': image_count,
'site_name': site_name,
}
def is_shown(self):
return permission_policy.user_has_any_permission(
self.request.user, ['change', 'add']
)
#hooks.register('construct_homepage_summary_items')
def add_corrected_images_summary_panel(request, items):
"""Replaces the Images summary panel to hide variants."""
for index, item in enumerate(items):
if item.__class__ is ImagesSummaryItem:
items[index] = CorrectedImagesSummaryItem(request)
This actually works fine, I am now showing the proper images count on the summary section but I am wondering is there a better way to query the collections of the user? Are these querysets right?
permissions = Permission.objects.filter(
content_type=ContentType.objects.get_for_model(get_image_model()),
codename__in=['change_image', 'add_image'])
collections = Collection.objects.filter(
group_permissions__group__in=self.request.user.groups.all(),
group_permissions__permission__in=permissions
).distinct()
Update
I ended up customizing the queryset for the images selection in order to only show the images within the collection the user was having access to.
In addition of the first function, I added the following code in my wagtail_hooks.py file.
#hooks.register('construct_image_chooser_queryset')
def show_collection_images_only(images, request):
# Show only the images from the collection the User has access.
collections = get_collections_from_group_permissions(request.user, ['change_image', 'add_image'])
images = images.filter(collection__in=collections)
return images
The get_collections_from_group_permissions is just a simplified function that returns exactly the Collection out of the Groups permissions the User has.
def get_collections_from_group_permissions(user, permissions):
"""
This function gets the Collections from the user groups permissions.
:param user: the user
:param permissions: the requested permissions on a Collection object
:returns: the Collections the selected User has access rights for.
"""
permissions = Permission.objects.filter(
content_type=ContentType.objects.get_for_model(get_image_model()),
codename__in=permissions)
collections = Collection.objects.filter(
group_permissions__group__in=user.groups.all(),
group_permissions__permission__in=permissions
).distinct()
return collections
With this in place, the Summary Item for the images is the number of images within the collections the User can access and when he clicks on a ImageChooserField and gets to the Image chooser, he only gets to see what is in the collections he has been granted access.
This logic is already implemented in Wagtail's permission_policy class, so this could be reduced to:
image_count = permission_policy.instances_user_has_any_permission_for(
self.request.user, ['change', 'add']
).count()
(Incidentally, the reason Wagtail itself doesn't take permissions into account when displaying this figure is that all users can see the complete set of images via the chooser popup - there's currently no 'choose' permission - so showing a reduced number would be misleading. See the discussion at https://github.com/wagtail/wagtail/issues/5129)

Adding multiple owners for Google App Maker records

I need help with Google App Maker data model security. I want to set multiple owners for a single record. Like the current user + the assigned admin + super admin.
I need this because all records can have different owners and super-owners/admins.
I know that we can point google app maker to a field containing record owner's email and we can set that field to the current user at the time of the creation of the record.
record.Owner = Session.getActiveUser().getEmail();
I want to know if it is possible to have field owners or have multiple fields like owner1, owner2 and then assign access levels to owner1, owner2...
Or how can we programmatically control the access/security/permissions of records?
The solution I'd use for this one definitely involves a field on the record that contains a comma separated string of all the users who should have access to it. I've worked on the following example to explain better what I have in mind.
I created a model and is called documents and looks like this:
In a page, I have a table and a button to add new document records. The page looks like this:
When I click on the Add Document button, a dialog pops up and looks like this:
The logic on the SUBMIT button on the form above is the following:
widget.datasource.item.owners = app.user.email;
widget.datasource.createItem(function(){
app.closeDialog();
});
That will automatically assign the creator of the record the ownership. To add additional owners, I do it on an edit form. The edit form popus up when I click the edit button inside the record row. It looks like this:
As you can see, I'm using a list widget to control who the owners are. For that, it is necessary to use a <List>String custom property in the edit dialog and that will be the datasource of the list widget. In this case, I've called it owners. I've applied the following to the onClick event of the edit button:
var owners = widget.datasource.item.owners;
owners = owners ? owners.split(",") : [];
app.pageFragments.documentEdit.properties.owners = owners;
app.showDialog(app.pageFragments.documentEdit);
The add button above the list widget has the following logic for the onClick event handler:
widget.root.properties.owners.push("");
The TextBox widget inside the row of the list widget has the following logic for the onValueEdit event handler:
widget.root.properties.owners[widget.parent.childIndex] = newValue;
And the CLOSE button has the following logic for the onClick event handler:
var owners = widget.root.properties.owners || [];
if(owners && owners.length){
owners = owners.filter(function(owner){
return owner != false; //jshint ignore:line
});
}
widget.datasource.item.owners = owners.join();
app.closeDialog();
Since I want to create a logic that will load records only for authorized users, then I had to use a query script in the datasource that will serve that purpose. For that I created this function on a server script:
function getAuthorizedRecords(){
var authorized = [];
var userRoles = app.getActiveUserRoles();
var allRecs = app.models.documents.newQuery().run();
if(userRoles.indexOf(app.roles.Admins) > -1){
return allRecs;
} else {
for(var r=0; r<allRecs.length; r++){
var rec = allRecs[r];
if(rec.owners && rec.owners.indexOf(Session.getActiveUser().getEmail()) > -1){
authorized.push(rec);
}
}
return authorized;
}
}
And then on the documents datasource, I added the following to the query script:
return getAuthorizedRecords();
This solution will load all records for admin users, but for non-admin users, it will only load records where their email is located in the owners field of the record. This is the most elegant solution I could come up with and I hope it serves your purpose.
References:
https://developers.google.com/appmaker/models/datasources#query_script
https://developers.google.com/appmaker/ui/binding#custom_properties
https://developers.google.com/appmaker/ui/logic#events
https://developers-dot-devsite-v2-prod.appspot.com/appmaker/scripting/api/client#Record

Drupal7: real time link selected field to a text field in Drupal7

I'm creating a webform to build up a subscription form, on Drupal 7.65
Goal
What I need to do is: to select a role from a list, and automatically to display the associated name of that role, in a text field.
As I said, the name should be displayed into a not modifiable text field just below it.
Suppose valid, the following list (key => value)
Field: Department
business_manager|Business Manager
hr_consultant|Human Resources
training_developer|Training Developer
and from the time going on, the associated names, are respectively
Options can appear into text field hr_business_partner
Steve Abc
Gertrude Def
Sven Hgj Klm
Thus when the trainee selects "Human Resources", the name of "Gertrude Def" should appear into the text field below the select one.
I've attached a mokup to better understand what I do need.
IMPORTANT
I can't put the names into the list as value, because the association can change but old records should keep the previously registered associations
You can use hook_form_alter() and add a new select field with the paired key value list you need to the webform. And then use javascript to update which field value gets shown in the HR Business Partner field on change, which by the way would also need to be added via your hook_form_alter. You could use a taxonomy to maintain a list of Departments/Business partners which would populate your department and business partners.
Write some javascript to dynamically update your original fields not added through the form_alter, on change. I would suggest making two textfields in your webform components which will hold the value from your form alter added fields. So that these values selected by the user gets saved in your form.
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_client_form_####"){
$form['#attached']['js'] = array(drupal_get_path('module','MODULENAME') . '/js/webform.js');
$form['hr_dept'] = array(
"#type" => "select",
"#options" => array("business_manager"=>"Business Manager", "hr_consultant"=>"Human Resources"),
);
$partners = taxonomy_get_tree(#); //the VID of the taxonomy
$list = array("0"=>"None"); //first option
foreach($partners as $tid => $partner){
$list[$partner->tid] = $partner->name;
}
$form['hr_partner'] = array(
'#type' => 'select',
'#options' => $list,
);
}
}
In your javascript file, /js/webform.js you can include all your logic to check for which value is selected on the Department field and then display the correct value in the Partners fields. At the same time, updating the original fields you've added as textfields in the webform components UI.

What is the variable that refers to the number of likes on sharepoint 2013?

I want to write a request on the search result request webpart. My request should enables me to retrieve all documents that have the biggest number of likes. There is no variable for the number of likes proposed on the drop list while writing a request , that why I decided to set a refinableInt00 variable and give it the value : LikesCount but it doesn't work? it means that LikesCount doesn't exist as a variable on sharepoint so what is the variable on sharepoint that would enable me to have the number of likes?
You can get the number of likes using the listitem property "Number of Likes"
This is a code from a Sample console application
using (SPSite site=new SPSite("your site URL"))
{
using (SPWeb web=site.OpenWeb())
{
SPList list = web.Lists["Your List Name"];
foreach (SPListItem item in list.Items)
{
//Print the number of likes
Console.WriteLine(item["Number of Likes"].ToString());
}
}
}
I know this is old but I had the same question. The problem is the LikesCount property does not default to Sortable. To fix this:
-Open up Central Administration
-Go to Search Service Application
-Click on Search Schema
-Locate the "LikesCount" property and click edit
-Scroll down to Sortable and change to Yes
-Run a full crawl on your content source
Ratings for list must be enable.
List -> List settings -> Rating settings ->
Allow items in this list to be rated?
yes ? no
and
Which voting/rating experience you would like to enable for this list?
Likes ? Star Ratings
After that you can access likes by "Number of Likes" field name "LikesCount".
"Number of Ratings" field name "RatingCount"

Salesforce: Picklist(multiple) with dynamic value

I have add custom field to Account with picklist(multiple) in Salesforce. But the values of picklist should be dynamically generated from other object. If not, is it possible to push values in picklist from native app(which is written in ruby)?
I dont think the standard controller supports dynamically adding the possible picklist (select list) values.
What you could do is add a text field instead of a picklist and create a custom page with visualforce. Use the standard controller with an extension for your code.
Create a new custom object for holding picklist values for a field (could be reused for other fields). Populate it with the possible picklist values.
In the page controller, load the values for that field.
In visualforce, display a picklist for the custom field and load the values from the controller.
Add an extra input field for manual insertion if desired.
On save, insert the value of the picklist (or input box) into the custom field.
A more detailed guide can be found here
Why dont't you use a normal SelectOption List in Apex?
public List<SelectOption> getMyPicklist()
{
List<SelectOption> options = new List<SelectOption>();
List<Account> acc = [ Select Id, Name From Account Limit 10 ];
for(Account a : acc){
options.add(new SelectOption(a.Id,a.Name));
}
return options;
}
I had to update picklist with values from my database (and not from some visualforce page). So I authenticated account using Databasedotcom gem and it provides nice Api to iteract with Salesforce Objects (both Standard and Custom).
client.authenticate :token => "my-oauth-token", :instance_url => "http://na1.salesforce.com" #=> "my-oauth-token"
account = client.materialize("Account")
account_to_be_updated = account.find(account_id) # here account is same as Instance of Activerecord
account_to_be_updated.my_picklist = [value1; value2; value3; value4]
account_to_be_updated.save

Resources