create node wuth Term reference tag - database

I'am trying to add node programmatically with term reference Auto complete term widget (tagging) and add new tag my code not work
when I remove tag reference from code ,my node add successfully
I need add new tag each node
<?php
$bodytext = "body?";
$node = new stdClass(); // Create a new node object
$node->type = "programstvradio"; // Or page, or whatever content type you like
node_object_prepare($node); // Set some default values
// If you update an existing node instead of creating a new one,
// comment out the three lines above and uncomment the following:
// $node = node_load($nid); // ...where $nid is the node id
$node->title = "title";
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->uid = 1; // UID of the author of the node; or use $node->name
$node->body[$node->language][0]['value'] = $bodytext;
$node->body[$node->language][0]['format'] = 'filtered_html';
// I prefer using pathauto, which would override the below path
$path = 'node_created_on' . date('YmdHis');
$node->path = array('alias' => $path);
$node->field_tag_[$node->language][]['value'] ='tagggggggg';
$node->field_catprogram[$node->language][]['tid'] =12;
$file_path = drupal_realpath('C:\Users\Admin\Desktop\drupal.png'); // Create a File object'); // Create a File object
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => file_get_mimetype($file_path),
'status' => 1,
);
$file = file_copy($file, 'public://images'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images'
$node->field_image[LANGUAGE_NONE][0] = (array)$file; //associate the file object with the image field:
if($node = node_submit($node)) { // Prepare node for saving
node_save($node);
echo "Node with nid " . $node->nid . " saved!\n";
}
?>``

i find solution
$my_term_name = 'gggg';
$term_array = taxonomy_get_term_by_name($my_term_name);
if($term_array == array()){
//empty term ..
$term->name = $my_term_name;
$term->vid = 1;
taxonomy_term_save($term);
$term_array = taxonomy_get_term_by_name($my_term_name);
}
//get the first index of the array .
foreach ($term_array as $tid => $term_object)break;
$node->field_progna['und'][$tid] = (array)$term_object;

Related

I want to create xml data with attributes in codeigniter

I want to Create XML data with attribute using PHP array in codeigniter(I am using Google map and for that I have to create xml data but in attribute format and I am getting data in xml tag format, I am creating xml from my PHP array whoich I am creating from database using MySqli)
I have tried this.
public function display_xml()
{
$this->load->dbutil();
$data = $this->db->query('select * from markers');
$config = array (
'root' => 'markers',
'element' => 'marker',
'newline' => "\n",
'tab' => "\t"
);
$xml = $this->dbutil->xml_from_result($data, $config);
$this->output->set_content_type('text/xml');
$this->output->set_output($xml);
}
I am getting this output..
<markers>
<marker>
<id>1</id>
<name>Love.Fish</name>
<address>580 Darling Street, Rozelle, NSW</address>
<lat>-33.861034</lat>
<lng>151.171936</lng>
<type>restaurant</type>
</marker>
<marker>
<id>2</id>
<name>Young Henrys</name>
<address>76 Wilford Street, Newtown, NSW</address>
<lat>-33.898113</lat>
<lng>151.174469</lng>
<type>bar</type>
</marker>
</markers>
I want this output
<markers>
<marker id="1" name="Billy Kwong" address="1/28 Macleay Street, Elizabeth Bay, NSW" lat="-33.869843" lng="-151.225769" type="restaurant"/>
<marker id="2" name="Love.Fish" address="580 Darling Street, Rozelle, NSW" lat="-33.861034" lng="151.171936" type="restaurant"/>
</markers>
Here we need to use of createAttribute.
public function display_xml()
{
$this->load->dbutil();
$data = $this->db->query('select * from markers');
$markers_data = $data->result_array();
$this->output->set_content_type('text/xml');
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("markers");
$dom->appendChild($root);
foreach ($markers_data as $value)
{
// create child element
$marker = $dom->createElement("marker");
$root->appendChild($marker);
// create attribute node
$id = $dom->createAttribute("id");
$marker->appendChild($id);
// create attribute value node
$priceValue = $dom->createTextNode($value['id']);
$id->appendChild($priceValue);
// create attribute node
$name = $dom->createAttribute("name");
$marker->appendChild($name);
// create attribute value node
$nameValue = $dom->createTextNode($value['name']);
$name->appendChild($nameValue);
// create attribute node
$address = $dom->createAttribute("address");
$marker->appendChild($address);
// create attribute value node
$addressValue = $dom->createTextNode($value['address']);
$address->appendChild($addressValue);
// create attribute node
$lat = $dom->createAttribute("lat");
$marker->appendChild($lat);
// create attribute value node
$latValue = $dom->createTextNode($value['lat']);
$lat->appendChild($latValue);
// create attribute node
$lng = $dom->createAttribute("lng");
$marker->appendChild($lng);
// create attribute value node
$lngValue = $dom->createTextNode($value['lng']);
$lng->appendChild($lngValue);
// create attribute node
$type = $dom->createAttribute("type");
$marker->appendChild($type);
// create attribute value node
$typeValue = $dom->createTextNode($value['type']);
$type->appendChild($typeValue);
}
// save and display tree
echo $dom->saveXML();
}

node_load_multiple() can it go through the array?

I'm trying to do this to get the "floor" value of my node :
$type = "desk";
$floor = '2';
$nodes = node_load_multiple(array(), array('field_floor["und"][0]["value"]' => $floor, 'type' => $type));
I can get all my desk if I'm just doing $nodes = node_load_multiple(array(), array('type' => $type)); and I can find a deck with for exemple the title, but is it possible to go through the arrays to get the 'value' and check it in the query ?
Thank you for your answers.
To get value of a field you have multiple methods :
Field get item:
$nid = 2;
$node = node_load($nid);
$floor = field_get_items($node , 'node', 'field_floor');
$floor = reset($floor); // or loop on it, here take first value if multiple
echo $floor['value'];
https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_get_items/7.x
Entity metadata wrapper:
$nid = 2;
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
$floor = $wrapper->field_floor->value();
echo $floor;
https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers
Direct way :
global $language; // take current language
$nid = 2;
$node = node_load($nid);
echo $node->field_floor[$language->language][0]['value'];

Find all in CakePHP is dumping a semicolon in my view

I don't know what exactly is happening with my CakePHP app. It worked last week and I have literally changed nothing with that particular file.
When I use find 'all' in my controller it dumps a semi-colon in my view even if there is nothing in the view file.
Here is my code
$evts = $this->Event->find('all');
There is nothing in my view file. I don't know if it makes a difference but I'm using a json view.
As requested here is the complete code for the action
public function search(){
$this->Event->recursive = 2;
$conditions = array();
if(!empty($this->request->query['name'])){
$conditions = array('Event.name LIKE ' => "%" . str_replace(" ","%", $this->request->query['name']) . "%");
}
if(!empty($this->request->query['home'])){
$conditions = array('Event.home_team_id' => $this->request->query['home']);
}
if(!empty($this->request->query['away'])){
$conditions = array('Event.away_team_id' => $this->request->query['away']);
}
$limit = 25;
if(!empty($this->request->query['limit'])){
$limit = $this->request->query['limit'];
}
//$evts = $this->Event->find('all',array('conditions'=>array($conditions),'order' => array('Event.start_time'),'limit'=>$limit));
$evts = $this->Event->find('all');
$this->set('events',$evts);
}
Everything in the view has been commented out ... but here is the code anyway
$results = array();
$i = 0;
foreach ($events as $event) {
$results[$i]['id'] = $event['Event']['id'];
$results[$i]['label'] = $event['Event']['name'] . "(" . date_format(date_create($event['Event']['start_time']), 'D, jS M Y') . ")";
$results[$i]['value'] = $event['Event']['name'];
$results[$i]['home_team_name'] = $event['HomeTeam']['name'];
$results[$i]['away_team_name'] = $event['AwayTeam']['name'];
$results[$i]['sport_name'] = $event['Sport']['name'];
$results[$i]['tournament_name'] = $event['Tournament']['name'];
$results[$i]['start_time'] = $event['Event']['start_time'];
$results[$i]['img'] = $event['Event']['img_path'];
$results[$i]['listener_count'] = 0; //TODO Get the follower count
$i++;
}
echo json_encode($results);
Display
If you changed nothing with that particular file then perhaps you have installed callbacks (either in the controller or in the model) that echo that ";". Look for 'afterFind' calls...
Search your model and your controller folders for "echo statement". In fact you should search your entire app folder except for the Views folder.

Drupal node_save is not saving fields

I have just stumbled upon very strange behaviour. In my code I am saving new node with some fields. I must say this used to work perfectly well. However now it just stopped working. I use the devel module to print out the content of objects and variables and this is my code:
dsm($node);
node_save($node);
dsm($node);
The first dsm() function displays node object as I created it and as it is supposed to be. The second dsm() function displays entirely correct node object with nid populated, fields content, etc... However, none of the fields is saved in database although there is new record in {node} table. Also, node_save does not generate any kind of error.
Any idea what is happening here?
This is the function storing the node:
function manhattan_incident_save($data) {
if ($data['nid']) {
$node = node_load($data['nid']);
manhattan_compare_changes($node, $data);
}
else {
$node = new stdClass();
$node->type = 'incident';
node_object_prepare($node);
$node->title = manhattan_create_incident_id();
$node->language = LANGUAGE_NONE;
}
$node->field_incident_popis[$node->language][0]['value'] = $data['field_incident_popis'];
$node->field_incident_popis[$node->language][0]['safe_value'] = check_plain($data['field_incident_popis']);
$node->field_incident_agent[$node->language][0]['uid'] = isset($data['field_incident_agent'])?intval($data['field_incident_agent']):NULL;
$node->field_incident_zdroj[$node->language][0]['tid'] = intval($data['field_incident_zdroj']);
$node->og_group_ref[$node->language][0]['target_id'] = $data['field_incident_oblast']?intval($data['field_incident_oblast']):variable_get('manhattan_public_form_default_area_nid', NULL);
$node->field_incident_riesitel[$node->language][0]['uid'] = isset($data['field_incident_riesitel'])?intval($data['field_incident_riesitel']):NULL;
$node->field_incident_typ[$node->language][0]['tid'] = intval($data['field_incident_typ']);
$node->field_incident_doplnenie[$node->language][0]['nid'] = intval($data['field_incident_doplnenie']);
$node->field_incident_sposob_kontaktu[$node->language][0]['value'] = $data['field_incident_sposob_kontaktu'];
$node->field_incident_dovod_vzniku[$node->language][0]['tid'] = intval($data['field_incident_dovod_vzniku']);
if ($data['field_incident_suvisiaci_zaznam']) {
foreach ($data['field_incident_suvisiaci_zaznam'] as $file) {
$result = manhattan_save_files($file);
if ($result) {
$node->field_incident_suvisiaci_zaznam[$node->language][] = array('nid' => $result);
}
}
}
// now create/save the customer
if (function_exists('customer_customer_save')) {
$customer = $data;
$customer['nid'] = $data['field_incident_customer'];
$result = customer_customer_save($customer);
if ($result) {
watchdog('upvs', $result['message'], array(), WATCHDOG_NOTICE);
}
else {
watchdog('upvs', t('There was a problem with saving customer %nid'), array('%nid' => $data['nid']), WATCHDOG_ERROR);
}
}
// now we store nid of saved customer to the node object
$node->field_incident_customer[$node->language][0]['nid'] = $result['nid'];
dsm($node);
node_save($node);
dsm($node);
if ($data['nid']) {
watchdog('upvs', t('Incident number %title has been succesfully saved.'), array('%title' => $node->title), WATCHDOG_NOTICE);
}
else {
watchdog('upvs', t('Incident number %title has been succesfully created.'), array('%title' => $node->title), WATCHDOG_NOTICE);
}
return $node;
}

How to save file object to a field after using"file_scan_directory"

I try to use file_scan_directory to scan some files and get some local path, and I want them to become an object and save into db, so I need file_save to do so.
file_save take an file object as parameter, and how can I suppose to success this procedure?
I have try file_save_upload, API doc mentioned the parameter "$source --- A string specifying the filepath or URI of the uploaded file to save.", however it seems not to read the path and always return null.
I also try to custom create a file object for file_save to run, it does work but don't think it's in correct drupal way, would there be any solution for:
file_scan_directory-> ???? -> file_save -> field_attach_update (* update a field with new fid once the file is save)
please help for this, thank you very much!
This snippet will scan some jpg files, save into db, and attach the files to a field.
function test_form(){
$node = node_load('61');
unset($node->field_image[$node->language]);
$files = file_scan_directory('public://testimport', '/^.*\.(jpg|JPG)$/');
//dpm($files);
//http://drupal.org/node/889058
foreach($files as $fileobj){
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'file')
->propertyCondition('uri', $fileobj->uri)
//$query->propertyCondition('uri', 'public://%', 'LIKE');
->execute();
if(isset($result['file'])){
dpm($result['file']);
$fid = reset($result['file'])->fid;
$fileobj = file_load($fid);
}else{
$fileobj->filemine = file_get_mimetype($fileobj->uri);
$fileobj = file_save($fileobj);
}
$node->field_image[$node->language][] = array(
'fid' => $fileobj->fid,
//'alt' => $node->title,
//'title' => $node->title,
'uid' => '1',
'filename' => $fileobj->filename,
'uri' => $fileobj->uri,
'filemime' => $fileobj->filemime,
'filesize'=> $fileobj->filesize,
'status' => '1',
);
}
if($node = node_submit($node)) { // Prepare node for saving
node_save($node);
echo "Node with nid " . $node->nid . " updated!\n";
}
}

Resources