It would be nice to be able to use php code and do a custom query

Comments

guillaumev’s picture

Status: Active » Needs review
StatusFileSize
new1.95 KB

Hi,

I needed the same functionality and wrote a patch to create it. Attaching it here...

mitchell’s picture

FYI: Similar discussion in #786208: Support PHP module.

guillaumev’s picture

StatusFileSize
new2.09 KB

Correcting an issue with my patch...

opensanta > can you explain what this is about ?

mitchell’s picture

@guillaumev: AFAICT, the general sentiment around the community is that enabling the php module opens up unnecessary security holes in Drupal. This is what I was pointing out by linking to the bad_judgement discussion. Another issue with putting code in the database is that it can lead to problems with development, as well as deployment. The convention in this case is to put your custom code in a site specific module along with your other custom functionality. Even more awesome though would be to generalize the functionality that you added and submit it as a patch or another module that everyone can use.

guillaumev’s picture

@opensanta: thanks a lot for the explanation (to be honest I like a lot better your second post with the explanation than the first one, though :-) ). I will create a plugin for the functionality and send it to the Feeds Tamper module in an issue.

So, should this issue be marked as closed (won't fix) ?

architectJpres’s picture

When you say functionality, does this mean only for a specific feature set or "transformation" ?

Does this mean the possibility of using custom php code is not possible?

guillaumev’s picture

architectJpres > I'm doing a feeds_tamper plugin which takes an OpenID as a parameter and needs to look up in the authmap table to return the uid of the corresponding user. The code by itself is very simple:

return db_result(db_query("SELECT uid FROM {authmap} WHERE authname = '%s' AND module = 'openid'", $field));
architectJpres’s picture

Yes okay, thats an interesting use case. How would you plan on integrating this functionality into feeds tamper? As a plugin for feeds tamper, or a plugin directly to the feeds module?

Perhaps there is an easy way to allow feeds tamper to take on additional plugins using PHP? or perhaps you would be so kind to elaborate how you create the module.

Unfortunately our use case is more obscure as we call for a custom datatable field. We have to concatenate 3 feilds and more...

nicolash’s picture

Quick pointer to how to add a custom Feeds Tamper plugin: #982066: Example of hook_feeds_tamper_plugins()

brycesenz’s picture

StatusFileSize
new1 KB

@architectJpres -

I wrote a similar custom plugin (attached), which might work for your use case. It allows you to use any mapped field in your PHP code, and uses drupal_eval to execute the code.

twistor’s picture

I'm not entirely opposed to a php eval plugin. My reluctance stems from losing potential plugin ideas from people writing one off scripts instead of proper plugins. Although undocumented, plugins are rather simple to write and should be the preferred method of using even simple custom code.

uwe_a’s picture

Some things cannot be done without a php eval plugin, maybe a suggestion to conribute code can be set there, but in my case for example my code is specific to my structure, basically to compute nid to be used in a node reference field based on a cck field value. i support integrating this into feeds tamper.

uwe_a’s picture

I'm using php_compute by brycesenz, i have multiple values, and im trying to get/return them, maybe this is not the correct location to ask this, but does that code allow that ?

please ...

EDIT: i did it by writing my own plugin !

tekken’s picture

I get the following error using the php_compute plugin:
Fatal error: Cannot use object of type FeedsImporter as array in /sites/all/modules/feeds_tamper/plugins/php_compute.inc on line 18

EDIT: line 9 has to be changed to:
function feeds_tamper_php_compute_form($importer, $element_key, $settings)

tekken’s picture

I couldn't access the $field variable using the php_compute plugin because it uses drupal_eval. This patch turned out to work better for my purposes because it gives access to the field.

zazinteractive’s picture

It works the first time I make the plugin but any changes I make to the php code are not used. It still uses the old code

zmz’s picture

tekken
>EDIT: line 9 has to be changed to:
>function feeds_tamper_php_compute_form($importer, $element_key, $settings)

thx

----

by the way,
what syntax of php_compute.zip
in two (or three) simple examples
plz share

brycesenz’s picture

@zmz - I'm not entirely sure what you're asking regarding syntax. What are you trying to do? Frankly, writing custom plugins is pretty easy, so you might just have better luck going that route (as demonstrated by post #13)

zmz’s picture

thx
done with pure php

millenniumtree’s picture

Thanks very much for this! Sometimes, you just gotta CODE IT!!

I created a little stub module with a couple of parser functions in it so I could modify the title and description fields, conditionally for certain feed URLs. It was working great, except that it would bomb on certain feed items.

Here's what the code looked like.

return _feed_munger_process_description('[url]', '[description']);

A description with a single quote would fail miserably, writing a PHP error out to the description instead of the feed item content.
If I switched to double quotes, I had the same problem, but with different feed items.

To solve this, I had to somehow store the contents of the token into a variable, without using quotes!
I came up with an awesome, but somewhat ugly, little hack.

ob_start();
?>[description]<?php
$description = ob_get_contents();
ob_end_clean();
return _feed_munger_process_description('[url]', $description);

* Open an output buffer with ob_start().
* Close the PHP tag so we can print the token as raw text
* Re-open the PHP tag
* Save the buffer contents to a variable with ob_get_contents()
* Clear the buffer with ob_end_clean() so it doesn't get printed.
Now we can pass our $description variable into the parser function.

Yeah, of course there is a more elegant way to do this by fixing the plugin itself, but I needed a solution right away and this works like a charm!

"But my mom says I'm cool." --Milhouse

lookatthosemoose’s picture

For my PHP code, I had to use:

<?php return call_user_func('custom_module_do_whatever',$field);?>
instead of
<?php return custom_module_do_whatever($field);?>

In order to get access to the $field variable. I found in the latter example, the value of $field was NULL when inside the custom_module_do_whatever() function.

puhig’s picture

Hi,
From Feeds OAI , i have a $fiels with some id's but whent it is imported i want to get its description so i do it :

$sql = " select name  from {term_data} t  where t.description = '%s'";
$results = array();

foreach ($field as $f) {
$resource = db_query($sql,$f);
$row = db_result($resource);
$results[] = $row;
}
return $results;

It's ok, but when it return a array on cck is stored 'Array', not data

lookatthosemoose’s picture

puhig: It's ok, but when it return a array on cck is stored 'Array', not data

What you'll need to do is use eval() to turn the string that is returned by feeds_tamper into an array (feeds_tamper can ONLY return a string)
So your feeds_tamper php looks like:

<?php 
$results = call_user_func('custom_module_function',$field); //returns an array
return var_export($results); //returns the array in literal string format like
                                        // " '1'=>'cat', '2'=> 'dog', '3' => 'poop' "
?>

Then, whereever you are using the results of the feeds_tamper php, you need to use eval()
to turn the literal string representation of the array into an actual array like:

eval("\$myarray = array($value);");  //where $value is the returned string from feeds tamper.

That'll set the value of $myarray to the array represented by $value.

Then,
The function you will likely looking that receives back the response from feeds tamper is feeds mapper hook.
For whatever mapper you are using, there will be a function hook_feeds_set_target($node, $target, $value) in the .inc file.
I was using nodereference... see code below:

/**
 * Implementation of hook_feeds_set_target().*/
function nodereference_feeds_set_target($node, $target, $value) {
  // Determine whether we are matching against title, nid, URL, or GUID.
  list($target, $match_key) = explode(':', $target, 2);

  // Load field definition.
  $field_info = content_fields($target, $node->type);

   // create the array!
   eval("\$value = array($value);"); //here!

   // Allow for multiple-value fields.
  $value = is_array($value) ? $value : array($value);
}
cyrilg’s picture

Title: PHP code » Php compute patch to use with Drush
StatusFileSize
new2.91 KB

Here is the patch taken from comment #10 and including the modification from comment #14. The following allows to use drush in your make file to apply the patch :

;patch to add php_compute plugins to feeds_tamper
projects[feeds_tamper][patch][] = "http://drupal.org/files/issues/php_compute.patch"

dgastudio’s picture

i'm sorry, but after apply the patch, php_compute widget doesnt appear.

up. sorry, my fault, it's for d6.

Can somebody reroll this patch for d7? thanks

kenorb’s picture

Status: Needs review » Closed (duplicate)