What I am trying to do is to enable/disable revision according to the selected taxonomy term in the content type that I have created i.e. when a user add the content the user can select the taxonomy term field(may be a select field) according to the selected option I want to enable/disable the revision. How can I do this?
Turn off the create new revision setting for the content type.
Then in hook_form_alter add a new submission handler before the main one:
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
//drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'CONTENT_TYPE_node_form':
//dpm($form);
$form['actions']['submit']['#submit'][] = 'revision_control_node_form_submit';
$form['actions']['submit']['#submit'] = array_reverse($form['actions']['submit']['#submit']); // reverse array to put our submit handler first
break;
}
}
Then in the new submit handler check if the taxonomy term has the correct value to save a new revision. I've not tried this next bit but according to this page putting
$node->revision = 1;
before node save will create a new revision.
node_save is called in node_form_submit and the node object is built in node_form_submit_build_node.
Looking at the other attributes like vid that belong to $form_state I would say an good educated guess would be to put $form_state->revision = 1; and see if that comes out as a property of the node after node_form_submit_build_node.
So you final new submit handler will look something like:
function revision_control_node_form_submit($form, &$form_state) {
if($form_state['values']['your_taxonomy_field'] == 'your_value') {
$form_state->revision = 1;
}
}
Now I've not actually tried any of this but even if it doesn't work I'm sure you will be on the right track... Good luck!
Related
I'm trying to add a preprocess hook to add classes based on taxonomy name to the body css of my drupal installation. I've managed to get all the information about the node based on doing some searching around and trial and error, but I'd like to take it to the next step and get all the taxonomy terms based on the particular node id.
My current preprocess code is follows:
function custom_preprocess_html(&$variables) {
// Add the node ID and node type to the body class
$body_classes = [];
$nodeFields =\Drupal::service('current_route_match')->getParameter('node')->toArray();
if (is_array($nodeFields) && count($nodeFields) > 0) {
if (isset($nodeFields['nid'])) {
$body_classes[] = 'node-' . $nodeFields['nid'][0]['value'];
}
if (isset($nodeFields['type'])) {
$body_classes[] = $nodeFields['type'][0]['target_id'];
}
}
$variables['attributes']['class'] = $body_classes;
}
It works fine and pulls down the information regarding the node. Based on the answer here it seems like all I should have to do is add the following line to get the taxonomy terms: $taxonomyTerms = $nodefields->get('field_yourfield')->referencedEntities(); but when I do so Drupal throws an error. I'll freely admit I'm new to Drupal 8, so any suggestions about where I'm going wrong (is field_yourfield not something that exists, maybe?) would be greatly appreciated.
If you are trying to get the referenced term names and add them as body classes, your approach seems a little bit off.
This is what I use:
function CUSTOM_preprocess_html(&$variables) {
// Entity reference field name.
$field_name = 'field_tags';
// Get the node object from the visited page.
// If the page is not a node detail page, it'll return NULL.
$node = \Drupal::request()->attributes->get('node');
// Let's make sure the node has the field.
if ($node && $node->hasField($field_name)) {
$referenced_entities = $node->get($field_name)->referencedEntities();
foreach ($referenced_entities as $term) {
$variables['attributes']['class'][] = \Drupal\Component\Utility\Html::cleanCssIdentifier($term->getName());
}
}
}
I would like to know if it's possible to populate a node fields with the values from form_state or something like that.
Basically what I do is to show a register form merged with a form from a Content Type. I do that by using field_attach_form(). Now on submit I create a node using:
$node = new stdClass();
$node->type = 'company';
$node->uid = 1;
node_object_prepare($node);
and now I would like to get all values from form_state and put them into the node.
Many thanks!
Hook your form, add new submit handler and place your code there.
I use Drupal 7 with Commerce module and making products more info page with links to prev/next products and others pages in product category.
http://postimg.org/image/8rbcsyye3/
Products preview/announce located at category page.
It's easy to do as views page with pager and number item per page equal to 1. But how can I jump from product preview page to product more info correctly? In that case it's need to navigate to specific page number in pager. How it's possible? or may be exist more appropriate way to solve it's task.
In Pager API I found the only function pager_find_page it's not even clue to pager_set_page. Maybe because it is described somewhere somehow, but I can't find it any way.
Answer to own question.
Solution for Drupal 5/6 (not sure about version)
Some other helpfull information
https://drupal.stackexchange.com/questions/1561/is-it-possible-to-dynamically-set-views-pager-settings
https://drupal.stackexchange.com/questions/16046/alter-pagination-start-index
After mixing that information I wrote simple solution for Drupal 7.
When user going to product node he can jump to any other product in current category.
That's why I create view with ajax enabled and contextual filter which return all node's ids from same product category by passing current page node id.
$n_id = intval(arg(1));
$nnm = db_query("select t.nid from {taxonomy_index} t left join {node} n on n.nid = t.nid where t.tid = (select t2.tid from {taxonomy_index} t2 where t2.nid = :vid) order by n.title", array(':vid' => $n_id));
$res= array();
if ($nnm->rowCount()) {
foreach ($nnm as $row) $res[]= $row->nid;
$handler->argument= implode('+', $res);
return true;
} else {
return false;
}
At custom module for my products full information view I added hook_views_query_alter.
Only in query_alter hook $view->query->where parameters are available and $view->query-pager not defined yet. It's mean that no need to rewrite default value for pager->current_page and product category's nids are avalable.
function [modulename]_views_query_alter(&$view) {
//$_GET['wtf'] -- variable to check first view load
if ($view->name == "view_name" && $view->current_display == 'view_display' && !isset($_GET['wtf'])) {
//Search current node id ($view->args[0]) in all category's nodes
$res= array_search($view->args[0], $view->query->where[0]['conditions'][0]['value'][':node_nid']);
if ($res) {
//$view->set_current_page($res);
//Set page id for pager
$_GET['page']= $res;
}
$_GET['wtf']= 2;
}
}
It is strange that there is no mention of such way to provide pager->current_page value.
Unlike pager->set_current_page method it can set page value at any views hook even if $view->pager not been initialized. It sounds pretty native for me.
And thank you for correcting my mistakes, I'm sorry for my bad english.
I have an entity reference field using Autocomplete widget type. I´d like to change the widget type to 'select list' for adding new node, and keep autocomplete when editing.
Have been two days I´m working on this. I didn´t found any solution.
One way I've done this is using hook_form_alter. Create a custom module (if you don't have one already (let's call it mymodule for now) and add the function:
function mymodule_form_alter(&$form, &$form_state, $form_id)
In there you can check the id to see which form is being processed, it should be something along the lines of mytype_node_form but you can also inspect it fairly easily by executing something like drupal_set_message(print_r($form_id, true)); in the function.
You can check to see if you're adding or updating by checking $form_state['node']->nid. After that you can modify the form by doing something like the following:
function mymodule_form_alter(&$form, &$form_state, $form_id)
{
// check to see if this is our form and it is a new node form (doesn't have an id yet)
if ($form_id == 'mytype_node_form' && !isset($form_state['node']->nid)) {
$form['field_coordinators']['und']['#type'] = 'select';
}
}
That's just a start mind you, you'll likely have to change or even remove other properties but you can look into these settings by setting the field to use a select list and inspecting the structure of $form, again.
When you get a node, how do you load the previous version (revision)?
I know how loading a revision but not how getting the previous revision number ($node->vid is the current revision).
thanks
Supposing that you have a node object $node, you can use the following code to get the previous revision.
$previous_vid = db_result(
db_query('SELECT MAX(vid) AS vid FROM {node_revisions} WHERE vid < %d AND nid = %d', $node->vid, $node->nid)
);
Once you have the previous revision, you can load the new node object with node_load(array('nid' => $node-nid, 'vid' => $previous_vid)).
The code should check if db_result() returns FALSE, in the case there isn't a previous revision.
To note that the field vid is global for each node; it doesn't contain the same value for different nodes.
Thanks all.
I found also an other solution:
$revisions = node_revision_list($node);
next($revisions);
if ($preview_key = key($revisions)) {
$preview_revision = $revisions[$preview_key];
$old_node = node_load($node->nid, $preview_revision->vid);
}
But if you have a lot of revision you get a big array.
If I understand what you're trying to do; you want to get the preview of the node after someone submits changes?
The preview button has its own submit handler, node_form_build_preview(). There, it creates a new node object using the data in $form_state and runs node_preview(), which returns the markup for the preview.
If you want to capture that preview when the user clicks the preview button, you'll need to use hook_form_alter to add another submit handler to the preview button:
$['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';
where mymodule_custom_preview is the name of your custom submit function. Take a look at node_form_build_preview() for guidance, but your submit function is going to look something like this:
function mymodule_custom_preview($form, &$form_state) {
$node = node_form_submit_build_node($form, $form_state);
$preview = node_preview($node);
}
Also take a look at node_form(), which gives you an idea of how the node form is structured. When you're all done, you're going to have code in your module that looks something like this:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if (strstr($form_id, '_node_form') !== FALSE) {
$['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';
}
}
function mymodule_custom_preview($form, &$form_state) {
$node = node_form_submit_build_node($form, $form_state);
$preview = node_preview($node);
// Do what you will with $preview.
}