Drupal7: Change node permission programmatically - drupal-7

I'm creating a custom Drupal 7 module and within my module I'm creating a node programmatically. In summary, it's something like this:
$node = new stdClass();
$node->type = 'donation';
node_object_prepare($node);
$node->title = 'Donation made on ' . date('r');
node_save($node);
How can I modify/set the permission for this dynamically created node so it's only accessible to admins?

I used Content Access module and it solved my problem.

Related

How to create folder base on users input using cakephp3

$user= $this->Users->newEntity();
I want to create a folder for every registered user
example if new user input michael
how to achieve this and will create a directory /img/users/michael/
$dir = new Folder(WWW_ROOT.'img'.DS.'users'.$user->username);
$path_data = $dir->create($dir);
Error: Cannot use object of type Cake\Filesystem\Folder as array
Create directory PATH will in in create() method
$dir = new Folder(WWW_ROOT.'img'.DS.'users'.$user->username);
$path_data = $dir->create($dir);
should be
$dir = new Folder();
$path_data = $dir->create(WWW_ROOT.'img'.DS.'users'.$user->username);
Details Here
Update
There is a directory separator missing. it should like
$dir = new Folder();
$path_data = $dir->create(WWW_ROOT.'img'.DS.'users'.DS.$user->username);
Thanks #ndm

Accessing location field in Drupal 7, via hosting entity object and wrapper

I'm using Location module (particularly its Location CCK part) with Drupal 7. Added location field 'field_location' to User (as an example of hosting entity), and initialized location values for test users in user edit interface. However, I'm unable to access location data of the current user:
global $user;
$user_id = $user->uid;
$loc = $user->field_location;
or:
$wrapper = entity_metadata_wrapper('user', $user_id);
$loc = $wrapper->field_location;
The statements with $loc don't work for object and wrapper (while both user object and wrapper are initialized successfully). Same for:
$loc = $wrapper->field_location[0];
$loc = $wrapper->field_location->raw();
I've read a number of posts on this topic, however haven't found a workable solution, would appreciate insights on this.
The location module, by itself, does not support the Entity API/Metadata Wrapper out-of-box. However, it is packaged with the Location Entity module, which will enable it for entity api support.
Once enabled,
$wrapper = entity_metadata_wrapper('user', $user_id);
$loc = $wrapper->field_location->value();
Works as expected.
If you want a quick workaround, you can also do:
$user_wrapper = entity_metadata_wrapper('user', $user_id);
$raw_user = $user_wrapper->raw();
$loc = $raw_user->field_location['und'][0];
This isn't elegant, but it's a solution without additional modules. Take your pick.

Create a node for every file with Drupal 7

I'm trying to create a new node for every file thats in a specific folder using Drupal 7.
A good example of what I'm trying to create is Youtube.
When I paste a video with the .mp4 extension in a specific folder, I want Drupal to scan that folder (I will tell Drupal when to scan, so this doesn't have to happen automatically), and create a node with that video in it. I will manually set the title, description, etc... myself using the admin interface, and publish it.
I know my way around in Drupal, and its modules, but I'm not an expert. I've been googling for awhile now and the only thing I could find was:
file_scan_directory($dir, $mask, $options = array(), $depth = 0)
I'm not asking for a complete copy/paste solution, I was just hoping someone could give me some tips, useful links or tutorials on how to do this.
To create a node for each video file you find in a directory, you can use code similar to the following one.
foreach (file_scan_directory($dir, '*.mp4', array('recurse' => FALSE) as $uri => $info) {
$body_text = 'Build the body text.';
$node = new stdClass();
$node->type = $node_type;
node_object_prepare($node);
$node->title = 'Node Created Programmatically on ' . date('c');
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0]['value'] = $body_text;
$node->body[$node->language][0]['summary'] = text_summary($body_text);
$node->body[$node->language][0]['format'] = 'full_html';
node_save($node);
}

drupal 7 file field formatter on custom node for javascript (to set up jQuizMe)

I have set up a module with custom node type (I called jquizme, after the javascript jQuizMe that I really like using). I set up two fields for the javascript files I need to supply to make it work (after the general jQuizMe-2.2.js file you need to add another two javascript files - one for settings and one for the quiz content).
Drupal saves the files as myjavascriptfile.js.txt - I tested them and they still work to make the jQuizMe interface - ok. the problem is, I want to add these files on the node page... the files will be different for each node. how can I access the files for the drupal_add_js() function so they will load the files for the node in question?
I tried setting up custom field formatters, but I don't know how to access the uri for the files of a given node automatically to put in the drupal_add_js() function (I can add a static file and it loads fine ... I did this with hook_node_view ( jquizme_node_view ).
So I just need a way to access the info for the files... how are they linked to each node? I can't find the connection.
As you probably noticed, I am a module writing newbie, and I probably won't understand much related to object oriented programming sorry, haven;t progressed to that level yet), but I am open to any answer. I am sure I left out important info, but this it already getting too long.
I also set up a special page earlier on to just see if I could get jQuizMe to work in Drupal so that is still in the code.
I have tried many answers (last six hours or so... too much to say here), the latest of which is using tokens, but that is not working. Here is what I have so far:
function jquizme_node_view($node, $view_mode, $langcode) {
switch ($node->type) {
case 'jquizme':
$items = field_get_items('node', $node, 'field_myfield', $node->language);
drupal_add_css(drupal_get_path('module', 'jquizme') . '/jQuizMe.css', >array('scope' => 'header'));
drupal_add_js(drupal_get_path('module', 'jquizme') . '/alert.js');
drupal_add_js(drupal_get_path('module', 'jquizme') . '/jQuizMe-2.2.js', >array('scope' => 'header'));
//drupal_add_js($tokens['node']['jquizme_js1_field'], array('scope' => >'header'));
//drupal_add_js($tokens['node']['jquizme_js2'], array('scope' => 'header'));
break;
}
}
Thanks in advance!
Can you try this?
// Let me assume that the field names of your two file fields
// are jquizme_js1_field and jquizme_js2_field for convenience..
function jquizme_node_view($node, $view_mode, $language) {
$items = field_get_items('node', $node, 'jquizme_js1_field');
_jquizme_add_js_from_filefield($items);
$items = field_get_items('node', $node, 'jquizme_js2_field');
_jquizme_add_js_from_filefield($items);
}
// Given the values of a filefield attached to some entity,
// adds them as JS files to the page.
function _jquizme_add_js_from_filefield($items = array()) {
foreach ($items as $item) {
$fid = &$item['fid'];
$file = file_load($fid);
if (!$file) {
continue; // Maybe the file got deleted..
}
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$path = $wrapper->realpath();
// Ensure that the path exists and that it is a Javascript file..
if (file_exists($path) && preg_match('\.js$', $path)) {
drupal_add_js($path, array('type' => 'file'));
}
}
}

How can I read the Active Directory schema programmatically

I did some programming for reading the data from Active Directory such as user account or Orgnization info and so on. The code below is like something what I did.
DirectoryEntry entry = new DirectoryEntry(
"LDAP://CN=Users,DC=domain,DC=com",
null,
null,
AuthenticationTypes.Secure
);
DirectorySearcher search = new DirectorySearcher(entry);
using (SearchResultCollection src = search.FindAll())
{
foreach (SearchResult result in src)
{
Console.WriteLine(result.Properties["name"][0] + " : " +
result.Properties["department"][0]);
}
}
The problem is how can I know what properties that target objects have then I can use them to filter the data before get it all.
Any ideas?
If you have a DirectoryEntry, you can inspect its .SchemaEntry:
DirectoryEntry entry = new DirectoryEntry("LDAP://......");
DirectoryEntry schema = entry.SchemaEntry;
This should - if you have the necessary permissions - give you access to the properties defined in the schema - things like MandatoryProperties or OptionalProperties:
foreach (var prop in schema.Properties.PropertyNames)
{
string propName = prop.ToString();
var propValue = schema.Properties[propName].Value;
}
Does that help you get started??
You might also want to have a look at BeaverTail - my C# open-source LDAP browser.
(source: mvps.org)
It will allow you to inspect any LDAP node and see all its properties.

Resources