Using event to publish in multiple locales/languages - concrete5

I'm trying to hook into the publish page type event so that I can publish the same page in multiple language sites at the same time using page attributes to select which languages to use. The code below runs fine without errors, but instead of creating a page duplicate in the target language I just get a new page draft in the original language. What am I missing here?
Code
<?php
Events::addListener('on_page_type_publish',
function($event) {
$page = $event->getPageObject();
if ($page->getPageTypeHandle() == 'blog_entry') {
$multilingualController = Core::make('\Concrete\Controller\Backend\Page\Multilingual');
$multilingualController->setPageObject($page);
$pageCollection = $page->getCollectionID();
$pageSection = \Concrete\Core\Multilingual\Page\Section\Section::getBySectionOfSite($page);
$pageLocaleText = $pageSection->getLanguageText();
$targetLocale = \Concrete\Core\Multilingual\Page\Section\Section::getByLocale('nb_NO'); //Will be set from page attribute
$targetLocaleCollection = $targetLocale->getCollectionID();
$createNewData = array(
'section' => $targetLocaleCollection,
'cID' => $pageCollection
);
$action = $multilingualController->action('create_new', $createNewData);
$theResult = $multilingualController->runAction($action, $createNewData);
}
});

Related

How to fetch data only for a particular user from a database with data multiple users in laravel

I am trying to display data from a database based on the logged in user in laravel.
If there is data for one user everything works well but if another user adds data to the table I get the error Cannot use object of type stdClass as array
Here is how I am getting the data in controller
public function viewBookings() {
$allProducts = Booking::get()->where('client', Auth::user()->name);
$products = json_decode(json_encode($allProducts));
foreach ($products as $key => $val) {
$service_name = Service::where(['id' => $val->service])->first();
$service_fee = Charge::where(['id' => $val->charge])->first();
$service_status = Status::where(['id' => $val->status])->first();
$products[$key]->service_name = $service_name->name;
$products[$key]->service_fee = $service_fee->total;
$products[$key]->service_status = $service_status->name;
}
return view('client.booking.view_bookings')->with(compact('products'));
}
The error is indicated at this line
$products[$key]->service_name = $service_name->name;
I have tested it by deleting the data for the other user and all works fine but If I return the other users data to the table I get that error
I would suggest removing this line
$products = json_decode(json_encode($allProducts));
and simply use
$products = $allProducts;
since what you are doing with the json_decode is the one creating the problems

drupal 7 modify taxonomy page with hook_alter_menu

i want to change or modify all taxonomy pages i write this piece of code in template.php in my bartik theme but some weird things happens.
the $term argument in second function "bartik_term_page" is null.
what is missing?
and it sometimes an error like this
it gives an error.
Warning: Parameter 1 to bartik_term_page() expected to be a reference, value given in menu_execute_active_handler() (line path\includes\menu.inc).
function bartik_menu_alter(&$menu) {
$menu['taxonomy/term/%']['page callback'] = 'bartik_term_page';
$menu['taxonomy/term/%']['access arguments'] = array('access content');
}
function bartik_term_page(&$term){
$voc = taxonomy_vocabulary_load($term->vid);
var_dump( $term ); die();
// here you generate the actual content of the page
// could be done e.g. with an entityfieldquery as follows
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->fieldCondition('field_category', 'tid', $term->tid);
$result = $query->execute();
if (!empty($result['node'])) {
$build['content']['nodes'] = node_view_multiple(node_load_multiple(array_keys($result['node'])), 'teaser'); // output the node teasers. you can control the markup of the 'teaser' view mode with a template in the theme folder
} else {
$build['content']['status']['#markup'] = t('No results found for term ID !tid.', array('!tid' => $term->tid));
}
return $build;
}
You're actually introducing a new router item there instead of overriding the existing one.
The path for the taxonomy page is taxonomy/term/%taxonomy_term, so...
function bartik_menu_alter(&$menu) {
$menu['taxonomy/term/%taxonomy_term']['page callback'] = 'bartik_term_page';
$menu['taxonomy/term/%taxonomy_term']['access arguments'] = array('access content');
}
Clear caches and you should be good to go.
Add the following line after the 'page callback' line in your bartik_menu_alter() function:
$menu['taxonomy/term/%']['page arguments'] = array(3);
This tells Drupal where to find the parameter in your URL (it's the third component).

Setting location data programmatically

I'm stuck on a problem that I've researched for several days with no luck and the answers here are usually spot on.
I have custom module code that adds a node from form supplied data:
$edit = array();
$edit['uid'] = $user->id;
$edit['name'] = $user->name;
$edit['status'] = 1;
$edit['taxonomy'] = array($term_id);
$edit['title'] = $Title;
$edit['body'] = $body;
etc...
and then saved with:
node_invoke_nodeapi($edit, $nType);
node_validate($edit);
if ($errors = form_get_errors()) {
print_r($errors);
}
$node = node_submit($edit);
node_save($node);
This all works perfectly. But I'm trying to add location data to each node based on a supplied (sanitized) zip field.
I have gmap and location modules installed and working. When I add the zip directly using the drupal content editor it all works. Even the views gmap. So i know versions and mods are all correct.
I've used this:
$location = array(
'country' => 'US',
'postal_code' => $zip,
);
$locationID = location_save($location);
and this:
$location['country'] = "US";
$location['postal_code'] = $zip;
$locationID = location_save($location);
with and without the country element.
And then in the node data init section (above) this:
$edit->locations[0]['lid'] = $locationID;
or
if($locationID) $edit['field_location'][0]['lid'] = $locationID;
or
if($locationID) $edit['location'][0]['lid'] = $locationID;
But none of this works. The submit will go through ok actually but no location data is saved. And no errors thrown.
Any help with this would be greatly appreciated.
I did get this to work, (in case anyone is having the same issue and stumbles upon this), by creating the node first and then adding the location data to the node with:
$locations = array();
$locations[0]['postal_code'] = $zip;
$criteria = array();
$criteria['nid'] = $node->nid;
$criteria['vid'] = $node->vid;
$criteria['genid'] = 'NAME OF NODE TYPE HERE';
location_save_locations( $locations, $criteria );
I guess location_save_locations is the correct way of doing it, not location_save.
Following your approach, as exposed by the wider location_save_locations() at line 4, you can update a location with location_save($locations[$key], TRUE, $criteria).
A few notes:
location_save return the lid of the saved location, or FALSE if the location is considered "empty."
Clean your cache or query the database ( mysql> SELECT * FROM location ORDER BY lid DESC limit 6 ) for a fresh view of the new entity location (ie: node_load data may be cached).
Alternatively, if you are handling an entity with a location field, you may try something cleaner like:
// Updated the location.
$node->field_location['und'][0] = $location;
// Save the node.
node_save ($node);

Create node programmatically with cck location field

I try to programmatically create a node of custom contenttype "location" in Drupal 6, with the node containing a location field (http://drupal.org/project/location) called "location" (yes I know, the nomenclature could be better, but I am just experimenting on this at the moment).
Creating the node works just fine, but I cannot find a way to set the contents for the location field - i.e. the node is created with all content but the value for then location field.
I try creating the node like this:
$newNode = (object) NULL;
$newNode->type = 'location';
$newNode->title = $locationName;
$newNode->uid = $userId;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;
$newNode->field_location[0]['street'] = 'Teststraße';
$newNode->field_location[0]['postal_code'] = '12345';
$newNode->field_location[0]['city'] = 'Musterstadt';
node_save($newNode);
The node gets created with the correct title, but the location fields remain unset.
How can I programmatically set the location-related fields?
Thanks in advance!
Wanted to add this as a comment, but it seems like putting code into the comment is rather problematic. So here we go: I changed the internas so that I do not use a cck field anymore, but use the default location option as suggested by googletorp.
The actual code to create a new location and assign this to a new node looks like this:
$location['street'] = "myStreet";
$location['postal_code'] = "12345";
...
$newLocationId = location_save($location);
$newNode = ...
$newNode->locations[0]['lid'] = $newLocationId;
node_save($newNode);
Thanks for the guidance :)
Instead of node_save, many people recommend using drupal_execute to programmatically submit the node edit form. This gives you the benefit of form validation.
See http://thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute for an excellent example of using drupal_execute. Don't forget to look at the comment http://thedrupalblog.com/programmatically-create-any-node-type-using-drupal-execute#comment-70 to see some additional info on CCK fields.
The advantage of drupal_execute is that you get form validation also. So after the drupal_executestatement you can see if there were any errors using form_get_errors ( http://api.drupal.org/api/function/form_get_errors/6 ). See snippet (pasted below) from http://civicactions.com/blog/cck_import_and_update for an example of using form_get_errors
$node->type = 'yourtype';
$values = array();
$values[...] = ...;
drupal_execute('yourtype_node_form', $values, $node);
$errors = form_get_errors();
if (count($errors)) {
// do something ...
}
Another very nice resource on programmatic submission of nodes using drupal_execute can be found at http://drupal.org/node/293663
I have done this, only not with a CCK field but the default location option you can add to nodes.
What I did to make it work, was to first save the location (there's an API function for it) and then add the location id from the saved location.
Sample code:
Note, $center is from an external source, so it's not Drupal related. I know all my locations are from Denmark in my example, so that part is just hardcoded.
When you don't use a CCK field, you don't need to save the location data on the node, instead you can just save the location and pair the location yourself. It's a quick solution, instead of running through the node form like suggested. For complex nodes, that might be the better choice, but when it's simple, this is done faster.
// Update the location data.
$location = is_array($node->location) ? $node->location : array();
$location += array(
'street' => $center->address->address2,
'city' => $center->address->zipName,
'postal_code' => $center->address->zip,
'country' => 'dk',
'country_name' => 'Denmark',
);
location_save($location);
// Insert location instance, if it's not set yet.
$criteria = array(
':nid' => $node->nid,
':vid' => $node->vid,
':lid' => $location['lid'],
);
if (!db_result(db_query("SELECT COUNT(*) FROM {location_instance} WHERE nid = %d AND vid = %d AND lid = %d;", $criteria))) {
db_query("INSERT INTO {location_instance} (nid, vid, lid) VALUES (%d, %d, %d)", $criteria);
}
For Drupal 7, saving as default location tab.
$location = array(
'latitude' => $row->gmapycord,
'longitude' => $row->gmapxcord,
);
$lid = location_save($location);
if ($lid) {
$entity->locations['0']['lid'] = $lid;
}
Inspired from: here

Locale module and language prefix in url

I had been working on module and well aware of hook_menu for passing url arguments to call back. For instance:
$items['webtv/block/%/playlist/edit/%'] = array(
...
'page callback' => 'drupal_get_form',
'page arguments' => array('webtv_playlist_form', 5, 2),
...
);
and callback as
function webtv_playlist_form($form_state, $fifth_arg, $second_arg){
...
}
Beside that arg() function is another utility to get url arguments by their positions.
$second_arg = arg(2);
$fifth_arg = arg(5);
When I enable locale module to make web as multilingual, URLs are classified with prefix as language symbol. Example:
en/webtv/block/%/playlist/edit/%
OR
nl/webtv/block/%/playlist/edit/%
This thing displaces the logical placement of arguments to right, now the correct placement of arguments (according to example) is:
$second_arg = arg(3);
$fifth_arg = arg(6);
How to set-up module independent of such argument placement issues?
Looking at Drupal core code (in example, node_menu()), menu callback using menu placeholders are not adjusted to work when locale.module is enabled, nor local.module alters the menus defined from other modules.
In fact, language_initialize(), called on Drupal bootstrap contains the following code:
case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
case LANGUAGE_NEGOTIATION_PATH:
// $_GET['q'] might not be available at this time, because
// path initialization runs after the language bootstrap phase.
$args = isset($_GET['q']) ? explode('/', $_GET['q']) : array();
$prefix = array_shift($args);
// Search prefix within enabled languages.
foreach ($languages as $language) {
if (!empty($language->prefix) && $language->prefix == $prefix) {
// Rebuild $GET['q'] with the language removed.
$_GET['q'] = implode('/', $args);
return $language;
}
}
The code is removing the language ID passed in the URL.
If locale.module is correctly set, the menu callback definitions should not be changed when the module is enabled.

Resources