hook_comment is not working for update operation in drupal - drupal-6

I am trying to modify comment body before saving or updating in drupal 6. I am trying to use following code:
function mymodule_comment(&$a1, $op) {
switch ($op) {
case 'insert':
case 'update':
$a1['comment'] = myfunction($a1['comment']);
break;
}
}
But it is not updating the comment body after save. I also tried following code and it works:
function mymodule_comment(&$a1, $op) {
switch ($op) {
case 'insert':
$a1['comment'] = myfunction($a1['comment']);
comment_save($a1);
break;
}
}
But in case of update it is showing error:
function mymodule_comment(&$a1, $op) {
switch ($op) {
case 'update':
$a1['comment'] = myfunction($a1['comment']);
comment_save($a1);
break;
}
}
Any idea or help is very appreciated.

Ok I got the work around. first create a hook_form_alter function:
mymodule_form_alter($form,$form_state,$form_id){
if ($form_id == 'comment_form') {
$form['#submit'][] = 'mymodule_comment_form_submit';
}
}
Then create hook_comment_form_submit function and do your modification:
mymodule_comment_form_submit($form, $form_state){
$form_state['values']['comment'] = myfunciton($form_state['values']);
comment_save($form_state['values']);
}
I dont know whether it is a good idea or not but I found few of post in which this approach is used.

Related

How to call auto of reutrn function in node js

On the time of page loaded get_switch() function which globally created on app.js page will be call then return a method. i want to execute these return methods.
demo.js
const return_functions = get_switch('BTC');
function get_btc()
{
console.log('btc');
}
function get_bch()
{
console.log('bch');
}
app.js
global.get_switch=function(coin_name){
switch(coin_name){
case 'BTC':
return 'get_btc()';
break;
case 'BCH':
return 'get_bth()';
break;
default:
console.log('default');
}
}
As shown in example above i have passed BTC in get_switch. and that function return us get_btc() function. so i want to call get_btc function on same time.
If this is not possible in this way so please guide me with your idea and suggest me how can i do this.
demo.js
var obj = {
get_btc: function get_btc() {
console.log('btc');
},
get_bth: function get_bth() {
console.log('get_bth');
}
}
const return_functions = get_switch('BTC');
if (return_functions) {
obj[return_functions]();
}
app.js
global.get_switch = function (coin_name) {
switch (coin_name) {
case 'BTC':
return 'get_btc';
break;
case 'BCH':
return 'get_bth';
break;
default:
console.log('default');
}
}
var get_switch=function(coin_name){
switch(coin_name){
case 'BTC':
get_btc();
break;
case 'BCH':
get_bth();
break;
default:
console.log('default');
}
}
get_switch('BTC');
function get_btc() {
console.log('btc');
}
function get_bch() {
console.log('bch');
}

C# Visual Studio's Text to Answer

This is kinda of a Noobie what about I am gonna ask but I am trying to get my Program to work I do not know how to Ask a question in a text box hit the button and it outputs the answer; I Have been researching this for a while I know how to get everything else to work.
So a simple Console app example:
static void Main()
{
bool exit = false;
string response;
while (!exit)
{
Console.Write("Command ('Exit' to end): ");
response = Console.ReadLine();
switch (response)
{
case "Hey":
Console.WriteLine("Welcome");
break;
case "unicorns":
Console.WriteLine("...are awesome!");
break;
case "Exit":
exit = true;
break;
default:
Console.WriteLine("Unrecognized command!");
break;
}
}
Console.Write("Press Enter to Quit");
Console.ReadLine();
}

Polymer Clone Objects

How can we clone an object in Polymer?
Example
this.colorsAsc.push({color: 'red'});
this.colorsDesc = this.colorsAsc.reverse();
this.colorsDesc[0].color = 'blue'; // Both will be blue doing this
I can do it in these many functionalities What is the most efficient way to deep clone an object in JavaScript? but I wonder if there is a way in Polymer to do that?
Angular does it https://docs.angularjs.org/api/ng/function/angular.copy
You can try the following hack:
this.colorsDesc = JSON.parse(JSON.stringify(this.colorsAsc.reverse());
I have had the same question here, where I have finally also found and posted an answer.
Short version:
newElement = element.cloneNode(true);
for(var i in element.properties) {
newElement[i] = element[i]
}
To clone a utility via Polymer
Full implementation:
(function(callBackFn) {
Polymer({
//Component Name
is: 'my-cloner',
properties: {
//Declare a published property
cloneableObject: { //Placeholder for Object to be cloned
reflectToAttribute: true,
type: Object,
notify: true
}
},
attached: function() {
//Hide if this component got attached
this.hidden = true;
},
getClone: function(incomingcloneableObject) { //Will be called to get the Clone
this.cloneableObject = this.cloneableObject || incomingcloneableObject;
switch (typeof this.cloneableObject) {
case "undefined":
return null;
break;
case "object":
var localClone = this.cloneNode();
return (localClone.cloneableObject);
break;
case "boolean":
return new Boolean(this.cloneableObject).valueOf();
//Other possible way
//return(this.cloneableObject ? true : false);
break;
case "number": //NaN is taken care of
return new Number(this.cloneableObject).valueOf();
//Other possible way
//return(this.cloneableObject * 1);
break;
case "string":
return new String(this.cloneableObject).valueOf();
//Other possible way
//return(this.cloneableObject + '');
break;
default:
return null;
}
}
});
//adding Util into window
callBackFn();
})(function() {
window.cloneUtil = document.createElement('my-cloner');
});
//To use this util
//window.cloneUtil.getClone();

function return values c#

I am trying to get to grips with C# having not coded for many years and my previous experience being in ANSI C.
I have read a number of books and searched online but one aspect is evading me and I am hoping someone here can help.
In the past I would declare a function and if there was a possibility of something not happening within the function (i.e. file not found etc.) declare the return to be an integer. I would then return 0 if all was well and a value if not. The value would correspond to where the function failed to execute fully and I could branch accordingly from where I called it.
if(function1())
{
// all my error stuff, maybe a switch/case etc.
}
All the examples I have found in C# seem to avoid this technique and I was hoping to get some understanding here.
Thanks in anticipation.
(I know I am a fossil). :)
Exceptions are the approach you use in C# and similar languages.
It goes like this:
try
{
function();
}
catch(FileNotFoundException e)
{
// File not found
}
catch(UnauthorizedAccessException e)
{
// User doesn't have right to access file
}
// etc...
To make this work, function shouldn't return a status code but instead throw an exception in case of an error.
Please note that the exceptions I illustrated in the code block above are thrown by the framework if you try to access a file and one of those errors is happening. So you don't actually have to do this yourself.
Furthermore, in C# there is no implicit conversion from integral values to bool, i.e. if(function()) is invalid, if function returns an int. You would need to write it like this:
if(function() != 0)
{
// all your error stuff
}
There's nothing to stop you doing this (though there are better ways of handling the errors - exceptions for example).
If you do want to carry on with this approach, the biggest problem you are having is that in C# you can't treat an integer as a boolean so your if test won't compile. What you need is:
if (function1() != 0)
{
}
But to check the value you'd need:
int result = function1();
switch (result)
{
case 1:
// Handle this case
break;
case 2:
// Handle this case
break;
default:
// All OK
break;
}
It would be better to return an enumerated type for each error case so that you don't have magic numbers, but exceptions are the way to go:
try
{
function1();
}
catch (SpecificException1 e1)
{
// Handle this case
}
catch (SpecificException2 e2)
{
// Handle this case
}
What you shouldn't have is a general exception handler:
catch (Exception e)
{
}
This just hides other potential problems.
If you want to follow that pattern of checking return value instead of managing errors, you better use enumarations than plain numbers.
For example:
public enum ResultType
{
Error = 0,
Success,
Waiting
}
public ResultType function()
{
if (still_waiting)
return ResultType.Waiting;
if (error_has_occured)
return ResultType.Error;
return ResultType.Success;
}
public void Main()
{
ResultType result = function();
switch (result)
{
case ResultType.Success:
MessageBox.Show("all is good");
break;
case ResultType.Waiting:
MessageBox.Show("still waiting...");
break;
case ResultType.Error:
MessageBox.Show("error has occurred");
break;
}
}
Behind the scenes, it's still using numbers but you put some meaning to each number.
if(function()==1)
{
}
int function()
{
int returnVal =0;
// do stuff
// if true return returnVal =1 else set returnVal =0;
return returnVal;
}

hook_comment not modifying data

I want to change the name of the comment from the users account name to use the real name of the user (I am using RealName and CCK fields).
This should be simple, however it is not correctly saving my values.
function mymodule_comment(&$a1, $op) {
switch ($op) {
case 'insert':
case 'update':
$realname = realname_get_user($a1['uid']);
if ($realname) {
$a1['name'] = $realname->name;
}
break;
case 'view':
$realname = realname_get_user($a1->uid);
if ($realname) {
$a1->name = $realname->name;
}
break;
}
}
If I dump a1 in both cases, the name is what I want it to be. This is not what is saved to the database, however.
How can I change the values so they are actually inserted?
Please provide info about realname_get_user function or else you can try below snippet
function mymodule_comment(&$a1, $op) {
global $user;
switch ($op) {
case 'insert':
case 'update':
$realname = $user->name;
if ($realname) {
$a1['name'] = $realname;
}
break;
case 'view':
$realname = $user->name;
if ($realname) {
$a1->name = $realname;
}
break;
}
}

Resources