How do populate username's in the select list. i added below code in the Allowed list textbox. But select list showing the PHP code instead of usernames.
global $user;
$sql = "SELECT name FROM users";
$res = db_query($sql);
while($row = db_fetch_array($res)){
$rows[] = $row['name'];
}
return $rows;
Note: Application running in D7.
As well i just followed below steps also.
Enable Core module PHP Filter.
Set/Check user permissions: admin/people/permissions#module-php.
Set/Check Text Formats: admin/config/content/formats.
Enable CCK Module. This step is often overlooked. However without CCK Module enabled you wont be able to enter (and thus execute) PHP-code in the Allowed-Values-List window
You may want to try Entity Reference - It works with CCK and allows you to reference Users: https://drupal.org/project/entityreference
Related
I have a few problems using Gedmo\DoctrineExtensions Translatable.
First is APY DataGrid - can't make it to show translated strings in grid on non default locale. If I use Translatable with default settings, all strings in grid are displayed in default language. If I implement Translatable in Entity and add annotations for table and other stuff, then I can see translated strings in grid, BUT after switching locales these stays the same. Seems like QueryCache is used, but can't find how to set not to use it. Here's a part for grid:
use APY\DataGridBundle\Grid\Source\Entity;
<...>
$source = new Entity('MainBundle:Entity');
$source->addHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
Of course it would be better to have translations without making annotations and separate entities for them.
Second problem is KNP pagination. Actually didn't dig into it, but similar problem.
The main problem is, when I run some query by translatable field of an entity. Let's say I have an Entity with name Krepšinis (it's basketball in Lithuanian) and I have translated this string to Basketball. Default language is LT. When in default language I perform a search, everything is ok, but if I change locale to EN and try searching for basket, it doesn't return any results and if I search for krep, it returns me Basketball. Code for search:
// Controller
$repository = $this
->getDoctrine()
->getManager()
->getRepository('MainBundle:Entity');
$query = $repository
->search($term)
->getQuery()
->useQueryCache(false)
->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
// Repository
public function search($query)
{
$qb = $this->createQueryBuilder('t');
$term = $qb->expr()->literal('%' . $query . '%');
$query = $qb->where($qb->expr()->like('t.name', $term));
return $query;
}
Any help is appreciated
if content is translated, you need to use query hints as you have already went through. But in order to have it cached for different locales, you need to set the locale hint to the query:
<?php
// hint the locale in order to make query cache id unique
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
'en' // take locale from session or request
);
Which is described in the documentation read it carefully.
ORM query cache is based on DQL, query parameters and query hints.
I'm using Drupal 7 + Rules. I would like to create a rule that unpublishes all nodes authored by a user when they have been given a particular role.
EVENT - After updating an existing user account
CONDITION - User has role(s): SelectedRole
ACTION - ???
BONUS: If this could be limited to nodes of a certain type, that would be even better.
If there is a better way outside of Rules to do this, I'm open to other ideas.
Thanks so much!
You can create a custom ruleset to loop through the nodes or a Views Bulk Operation action.
An easiest option is to add a custom PHP function on your rule (PHP > Execute custom PHP code). Of course you have to enable the php filter core module if you didn't already.
In the PHP action you have to get all the nids of the published nodes the current user and loop through them to unpublish them. I will use the EntityFieldQuery API class but you can use the database functions also.
// Get updated user id
$uid = $account -> uid;
// Get all nodes from user that are of NODE_TYPE and are published
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'NODE_TYPE')
->propertyCondition('status', 1)
->propertyCondition('uid', $uid);
$result = $query->execute();
$nids = array_keys($result['node']);
// Load all nodes in one go for better performance.
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
// set status property to 0 (unpublished)
$node->status = 0;
// re-save the node
node_save($node);
}
I would also suggest to add one more Condition for the user before the one you are using: User Has Roles: (NOT) SelectedRole so that the action do not run everytime the user profile is updated.
References:
Use Rules to Unpublish content based on Author's role
Publish unpublished node programmatically
Find nodes created by user (more programmatically)
I have a Drupal content type which contains a number of computed fields. Some (but not all) items are being added to this content type via a cron-triggered RSS feed importer. I'm trying to trigger computed field generation for new items in hook_cron. The following code grabs all items that haven't been tagged as 'submitted', loads and re-saves the node, and then marks the node as 'submitted'.
$query = db_select('node', 'n');
$query->fields('n', array('nid'));
$table_alias = $query->join('field_data_field_submitted', 'r', 'n.nid = r.entity_id AND r.field_submitted_value = 0');
$result = $query->execute();
foreach ($result as $record){
$q = $record->nid;
$n = node_load($q);
node_save($n);
$query = db_update('field_data_field_submitted')
->fields(array('field_submitted_value' => 1))
->condition('entity_id', $q)
->execute();
}
This code works the way I expect it to if I call it from a module-generated page (created using hook_menu with a page callback function). Nodes are resubmitted, and the computed field data is generated. When I put this code in my hook_cron function, the query works, it loops through the records and updates the 'submitted' value, but the computed fields are not computed. I'm confused as to why this would not get triggered in cron. Any help?
Doh! Finally realized that this was completely my own doing. Due to the nature of this content type, where we allow anonymous users to create new content, but explicitly do not trigger the computed fields when they create the content (long story, but short form is that authenticated users then verify & enhance this content, which is where the computed fields come in). So, as I was setting up the initial content, I disabled the computed fields for anonymous users (if $user->uid > 0), and completely forgot about that. Once I tweaked that logic to allow computed fields to be processed on import (triggering it with a field that has a value for the imported content, but not for other content), the problem was solved.
The cron run has access to the full bootstrap so there's no logical reason why your code would produce different results in that context.
That said, you're only updating the field_data_field_submitted table when you also need to update the field_revision_field_submitted table, so that might somehow account for the discrepancy.
Drupal provides an API for the field system so that these sorts of problems can be avoided completely. The same code you've used, rewritten the 'Drupal' way, would be:
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
->fieldCondition('field_submitted', 'value', 0);
$results = $query->execute();
if (!empty($results['node'])) {
$nodes = node_load_multiple(array_keys($results['node']));
foreach ($nodes as $node) {
$node->field_submitted[$node->language][0]['value'] = 1;
node_save($node);
}
}
I can't think of a good reason why the above code would fail on cron either so it might be worth giving it a whirl.
How to programmatically retrieve configuration of the specific rule?
I tried
$settings = rules_config_load('RULE_NAME');
It returns very basic info (name, ID etc) and empty "settings" array.
Cant also get it directly from DB .
It is stored in serialized array that can not be fully processed with the
unserialize() function
I got the same issue and end up here, here is a solution i found, you need to call actions() on your rule to access settings:
$rule = rules_config_load('RULE_NAME');
foreach ($rule->actions() as $action) {
$settings[] = $action->settings;
}
I have created a table in drupal to display the records. How can i add the edit link to each record so that it goes to an input form corresponding to the id for that record
function display($nid){
$query = db_query("select * from {contactus}");
$data = array();
$i = 0;
while($row = db_fetch_array($query)){
$data[$i] = $row;
$i++;
}
$output = theme_table(array('id','email','comment'),$data);
return $output;
}
You must implement the full CRUD-range, Create Read Update Delete. Right now you only have an index. For Drupal7 there is a good example in dbtng (from the examples)
For Drupal 6 I am not aware of such an example.
Basically the pattern is:
make hook_menu-items with callbacks, one for the index, Read, Update, Delete, Create.
The Read item simply shows the item (item/%id)
The Update shows a form to update the item (item/%id/edit). Form is pre-filled. See FormApi in Drupal for more information on forms.
The Delete shows a confirm_form() with a callback to delete the entry from the database.
The Create shows a form to create a new item. Form is empty.
But to answer your exact quesion, in Drupal you create a link with l(). l('foo', 'item/bar') Will create a foo.