Translate a nested Paragraph programmatically - drupal

i have translated nodes with a paragraph field (content already exists, all entities are translatable). I've added a nested paragraph field on the first paragraph field. My goal is to programmatically add translated nested paragraphs.
node (
* first paragraph field
** nested paragraph field
But when i run my update, the paragraphs on the translated nodes are still in the original language (not translated). What am i doing wrong?
$node = $nodeStorage->load($nid);
$paragraphs = $node->get('field_content')->referencedEntities();
// Initialize new $nested items - THIS PART WORKS.
foreach($paragraphs as $paragraph) {
$nested = Paragraph::create(['type' => 'my_type',]);
$nested->set('field_text', 'Some Value.');
$nested->save();
$paragraph->get('field_paragraphs')->appendItem($nested);
$paragraph->save();
$node->save();
}
}
// Add $nested translations - THIS PART DOESN'T WORK
$languages = $node->getTranslationLanguages();
foreach ($languages as $language) {
$langcode = $language->getId();
$translation = $node->getTranslation($langcode);
$paragraphs = $translation->get('field_content')->referencedEntities();
// Set first picture values.
foreach ($paragraphs as $paragraph) {
$paragraph_translation = $paragraph->getTranslation($langcode);
$nested = $picture->get('field_paragraphs')->referencedEntities();
$nested = reset($picture_item);
$newValues = [
'field_text => 'Translated value',
];
$nested->addTranslation($langcode, $values);
$nested->save();
$picture->save();
$translation->save();
}
}

Could you please try to access the field directly instead of using the get method?
like this:
// Add $nested translations
$languages = $node->getTranslationLanguages();
foreach ($languages as $language) {
$langcode = $language->getId();
$translation = $node->getTranslation($langcode);
$paragraphs = $translation->field_content->referencedEntities();
// Set first picture values.
foreach ($paragraphs as $paragraph) {
$paragraph_translation = $paragraph->getTranslation($langcode);
$nested = $picture->field_paragraphs->referencedEntities();
$nested = reset($picture_item);
$newValues = [
'field_text' => 'Translated value',
];
$nested->addTranslation($langcode, $values);
$nested->save();
$picture->save();
$translation->save();
}
}

Related

cakephp3: Save associated data

I want to write a easy Tagging-Plugin for cakephp3 applications. So lets say we have a model books and a model reviews. For each of this models it should be possible to attach Tags - just by adding a behavior (in a plugin): $this->addBehavior('Tag.Taggable').
I created two Tables in the Database: tags, tagged_tags.
Table tagged_tags:
id | tag_id | tagged_id |
1 | 1 | 1 |
2 | 2 | 1 |
tagged_id is the id of the tagged entity. The information which model it belongs to is in the other table.
Table Tags:
id | tag | model |
1 | book | App\Model\Table\BooksTable |
2 | nobook | App\Model\Table\ReviewsTable|
Obviously, only the first Tag belongs to a book.
class TaggableBehavior extends Behavior
{
// Some more code here
public function __construct(Table $table, array $config = [])
{
parent::__construct($table, $config);
$this->_table = $table;
$this->_table->belongsToMany('Tag.Tags', [
'joinTable' => 'tagged_tags',
'foreignKey' => 'tagged_id',
'targetForeignKey' => 'tag_id',
'conditions' => [
'Tags.model' => get_class($this->_table);
]
]);
}
}
Retrieving the data works perfectly. But saving is an issue:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'Tags.model' in 'where clause'
SQL Query:
SELECT TaggedTags.id AS TaggedTags__id, TaggedTags.tagged_id AS
TaggedTags__tagged_id, TaggedTags.tag_id AS TaggedTags__tag_id
FROM tagged_tags TaggedTags WHERE (tagged_id = :c0 AND Tags.model =
:c1)
I'm not so sure why cakephp performs a SELECT-query here, and I don't really care. Why this query causes an error is clear. But where is my mistake here? It has to do with the 'conditions' => ['Tags.model' => get_class($this->_table);. Without this, I can save data (but cant say which Tag belongs to a book or not)
EDIT: Some Additional Info
Here is the complete sql statement, displayed in the debug kit http://freetexthost.com/tc3s46nugi
controller code:
public function add()
{
$book = $this->Books->newEntity();
if ($this->request->is('post')) {
$book = $this->Books->patchEntity($book, $this->request->data);
if ($this->Books->save($book)) {
$this->Flash->success(__('The book has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The book could not be saved. Please, try again.'));
}
}
In the Behavior I have some logic (copy/pasted) form the Bookmarks-tutorial
public function beforeSave($event, $entity, $options)
{
if ($entity->tag_string) {
$entity->tags = $this->_buildTags($entity->tag_string);
}
}
protected function _buildTags($tagString)
{
$new = array_unique(array_map('trim', explode(',', $tagString)));
$out = [];
$query = $this->_table->Tags->find()
->where([
'Tags.tag IN' => $new,
'Tags.model' => $this->name()
]);
// Remove existing tags from the list of new tags.
foreach ($query->extract('tag') as $existing) {
$index = array_search($existing, $new);
if ($index !== false) {
unset($new[$index]);
}
}
// Add existing tags.
foreach ($query as $tag) {
$tag['count'] = $tag['count']+1;
$out[] = $tag;
}
// Add new tags.
foreach ($new as $tag) {
$out[] = $this->_table->Tags->newEntity(['tag' => $tag, 'model' => $this->name(), 'count' => 0]);
}
return $out;
}
Easy tagging plugin you will make if you create Tags model, and create a HABTM relationship with other models.
Here are simple guidelines.
add.ctp
Omit multiple select tags field, instead put tagging text field (eg. tagsinput). Add some jquery tagging plugin. When adding a new tag (keyword), it is immediately stored in the tags table via jquery post methods. If the keyword exists in the tags table, then do not store it again.
Behavior
In the beforeSave method processing value from tagging field.
If the value separated by a comma, you can use something like this (CakePHP 2):
public function beforeSave($options = array())
{
if(!empty($this->data['Article']['tagsinput'])) {
$tags = explode(',', $this->data['Article']['tagsinput']);
foreach ($tags as $tag) {
$tagKey = $this->Tag->find('first',array(
'recursive' => -1,
'fields' => array('id'),
'conditions' => array('Tag.name' => $tag)
));
$this->data['Tag']['Tag'][] = $tagKey['Tag']['id'];
}
}
return true;
}
This creates HABTM relationship and stores such values in the table articles_tags.
edit.ctp
Create a comma separated values:
<?php
$extract_tags = Hash::extract($this->data['Tag'],'{n}.name');
$tags = implode(',', $extract_tags);
echo $this->Form->input('tagsinput',
array(
'id' => 'tags',
'div'=>true,
'label'=>__('Tags'),
'class'=>'form-control input-lg',
'placeholder'=>__('Add keywords here'),
'value' => $tags
)
);
?>

Silverstripe - filter DataObject list by many many relationship

I have an Object that extends Page ("Thing") which has a many_many relationship with a DataObject ("Tag").
class Thing extends Page
{
static $many_many = array(
'Tags' => 'Tag'
);
}
class Tag extends DataObject
{
static $belongs_many_many = array(
'Things' => 'Thing'
);
}
I have an array of Tag IDs and I want to get a list of Things that have all of these tags attached.
The following should be possible...
$tag_ids = array(1,2,3,4);
$things = Thing::get();
$things->filter('Tags.ID', array($tag_ids));
...but this just returns an unfiltered list. Apparently this hasn't been implemented for relationships yet. So how can I do it?
I think if you are using older versions for SilverStripe 3, you need to use the ExactMatchMulti SearchFilter.
$tag_ids = array(1,2,3,4);
$things = Thing::get();
$things->filter('Tags.ID:ExactMatchMulti', $tag_ids);
I don't see an easy solution do do this directly with the ORM. But you should be able to solve this with some loops and filtering.
$tag_ids = array(1,2,3,4);
$things = Thing::get();
$results = new ArrayList();
$tags_count = count($tag_ids);
$matches = 0;
foreach ($things as $thing)
{
foreach ($thing->Tags() as $tags)
{
$matches = 0;
foreach ($tags as $tag)
{
if ( in_array($tag->ID, $tag_ids) )
{
$matches++;
}
}
if ( $matches === $tags_count )
{
$results->push($thing);
}
}
}
Although not tested, this should leave you with $things that contain those with all tags and more. (Assuming a thing can only be tagged once with the same tag).
For performance reasons you would probably be better off relying as much as possible on a sql query.
$tag_ids = array(1,2,3);
$objects = new ArrayList();
if (count($tag_ids)) {
$sql = "SELECT \"SiteTree\".*, \"Page\".*, \"Thing\".*,count(\"ThingID\") AS ThingIDCount FROM \"SiteTree\" ";
$sql.= "LEFT JOIN \"Page\" ON \"Page\".\"ID\" = \"SiteTree\".\"ID\" ";
$sql.= "LEFT JOIN \"Thing\" ON \"Thing\".\"ID\" = \"SiteTree\".\"ID\" ";
$sql.= "LEFT JOIN \"Thing_Tags\" ON \"Thing_Tags\".\"ThingID\" = \"SiteTree\".\"ID\" ";
$sql.= "LEFT JOIN \"Tag\" ON \"Thing_Tags\".\"TagID\" = \"Tag\".\"ID\" ";
$sql.= "WHERE \"TagID\" IN (" . implode(',', $tag_ids) . ") GROUP BY \"ThingID\" HAVING ThingIDCount >= " . count($tag_ids);
// Get records
$records = DB::query($sql);
foreach($records as $record) {
$objects->push(new Thing($record));
}
}
// Display the Things for demo purposes
foreach($objects as $thing){
Debug::Dump($thing);
}
NB I have added a left join to a Thing table, as I imagine you have some db fields on it, but drop the line (and the \"Thing\".* on the SELECT statement) if that's not the case
You can try to do the following:
Tag::get()->byIds($tag_ids)->relation('Things')
which will return a ManyManyList that you can iterate over, ie
foreach(Tag::get()->byIds($tag_ids)->relation('Things') as $thing){
Debug::Dump($thing); // A 'Thing' object
}
Hope this helps

Generate url aliasing based on taxonomy term

I have a vocab category and four terms within it. what i want to do is if content is tagged with a termin in particular say "term1" to have the url generated as word1/[node:title] and for all the other tags just the standard url formatting.
If i wanted the term in the url obviously id use pattern replacement but i want another word to be used if a particular tag is used
I can't think of an easy plug-and-play way of achieving this. You may have to create your own token for the "Default path pattern" in Pathauto's URL alias settings:
/**
* Implementation of hook_token_info().
*/
function MODULE_token_info() {
$info['tokens']['node']['node-term-path'] = array(
'name' => t('Node path by term'),
'description' => t('The path to a node based on its taxonomy terms.'),
);
return $info;
}
/**
* Implementation of hook_tokens().
*/
function MODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'node-term-path':
$items = field_get_items('node', $node, 'TAXONOMY_FIELD_NAME');
foreach ($items as $item) {
$tids[] = $item['tid'];
}
if (in_array(TID_OF_TERM1, $tids)) {
// Path for nodes with term1
$replacements[$original] = 'word1/'. pathauto_cleanstring($node->title);
}
else {
// Path for other nodes
$replacements[$original] = 'content/'. pathauto_cleanstring($node->title);
}
break;
}
}
}
return $replacements;
}
Found a simple way actually to anyone who need a similar solution use the module Entity Reference.
http://drupal.org/project/entityreference
I just created a new field for the user account select entity reference then you can choose any entity within drupal to reference.
(ie so you can select a term/content/anything)

Drupal 7 - Insert taxonomy into node object

I have a script which successfully creates new nodes. But I'm having trouble setting the taxonomy before saving.
I believe in Drupal 6 I would use this method.
$cat1_tid = taxonomy_get_term_by_name($data[$i]['cat1']);
$cat2_tid = taxonomy_get_term_by_name($data[$i]['cat2']);
$cat3_tid = taxonomy_get_term_by_name($data[$i]['cat3']);
$node->taxonomy = array($cat1_tid, $cat2_tid, $cat3_tid);
I think in Drupal 7 I would do this (my field name is Catalog)
$node->taxonomy_catalog['und'][0] = array($term1Obj, $term2Obj);
taxonomy_get_term_by_name doesn't seem to return the correct object to insert into the node object.
If anyone can shed some light, appreciated.
Thanks
EDIT
Solution:
// Taxonomy
$categories = array($data[$i]['cat1'], $data[$i]['cat2'], $data[$i]['cat3']);
foreach ($categories as $key => $category) {
if ($term = taxonomy_get_term_by_name($category)) {
$terms_array = array_keys($term);
$node->taxonomy_catalog[LANGUAGE_NONE][$key]['tid'] = $terms_array['0'];
}
}
Below is some quick-and-dirty code I used recently to import "command" nodes into a site. Mid-way down, the foreach loop takes care of creating and assigning terms, as needed.
$command = new stdClass;
$command->language = LANGUAGE_NONE;
$command->uid = 1;
$command->type = 'drubnub';
$command->title = $line['0'];
$command->body[LANGUAGE_NONE]['0']['value'] = $line['1'];
$command->url[LANGUAGE_NONE]['0']['value'] = trim($line['2']);
$command->uses[LANGUAGE_NONE]['0']['value'] = $line['3'];
$tags = explode(',', $line['4']);
foreach ($tags as $key => $tag) {
if ($term = taxonomy_get_term_by_name($tag)) {
$terms_array = array_keys($term);
$command->field_tags[LANGUAGE_NONE][$key]['tid'] = $terms_array['0'];
} else {
$term = new STDClass();
$term->name = $tag;
$term->vid = 1;
if (!empty($term->name)) {
$test = taxonomy_term_save($term);
$term = taxonomy_get_term_by_name($tag);
foreach($term as $term_id){
$command->product_tags[LANGUAGE_NONE][$key]['tid'] = $term_id->tid;
}
$command->field_tags[LANGUAGE_NONE][$key]['tid'] = $tid;
}
}
}
node_save($command);
Here you are, this code successfully add a new term to the node before the node is created.
$my_term_name = 'micky';
$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_tag['und'][$tid] = (array)$term_object;
Perhaps my experience is unique, but I found that using
$term = taxonomy_get_term_by_name($tag)
$tid = $term->tid;
caused an error.
I found that after $term is saved, there is no need to fetch the newly created term.
The $term object is updated to include the new tid.
Any answer that use LANGUAGE_NONE or 'und' to alter a field is not the proper way of doing it as it assumes that the drupal site is one language. The proper way to edit a field is to use entity_metadata_wrapper.
$node_wrapper = entity_metadata_wrapper('node', $node);
// If you have Entity API [entity] module installed you can simply.
$node_wrapper = $node->wrapper();
// It is good practice to check the terms in the field before adding
// a new one to make sure that the term is not already set.
$term_ids_current = $node_wrapper->taxonomy_catalog->raw();
if (!in_array($term_new_id, $term_ids_current)) {
$node_wrapper->taxonomy_catalog[] = $term_new_id;
}
// To add multiple terms iterate an array or terms ids.
$term_ids_current = $node_wrapper->taxonomy_catalog->raw();
$tern_new_ids = array(1, 2, 3);
foreach ($term_new_ids as $term_new_id) {
if (!in_array($term_new_id, $term_ids_current)) {
$node_wrapper->taxonomy_catalog[] = $term_new_id;
}
}
// To remove a term.
$term_ids_current = $node_wrapper->taxonomy_catalog->raw();
$delta = array_search($term_remove_id, $term_ids_current);
if (is_int($delta)) {
$node_wrapper->taxonomy_catalog->offsetUnset($delta);
}
// To replace all terms.
$term_new_ids = array(1, 2, 3);
$node_wrapper->taxonomy_catalog->set($term_new_ids);
// To get all the fully loaded terms in a field.
$terms = $node_wrapper->taxonomy_catalog->value();
// At the end make sure to save it.
$node_wrapper->save();

Search hook for filtering results?

I have been going through the docs and source code looking for something without luck.
Is there a Drupal 6 hook that gets called after hook_search(), but before the $results gets handed off to the template system?
I need to do a fairly custom pruning and reordering of results that get returned. I could just reimplement hook_search(), but this seems like overkill.
Thanks.
There isn't; search_view() (which displays the results) calls search_data(), which invokes hook_search() then immediately themes the results. Re-implementing hook_search() is probably the most straightforward route.
With that said, you could instead implement hook_menu_alter() and have the search page call your custom function instead of calling search_view() (and subsequently calling search_data()). Something like:
function test_menu_alter(&$items) {
$items['search']['page callback'] = 'test_search_view';
foreach (module_implements('search') as $name) {
$items['search/' . $name . '/%menu_tail']['page callback'] = 'test_search_view';
}
}
// Note: identical to search_view except for --- CHANGED ---
function test_search_view($type = 'node') {
// Search form submits with POST but redirects to GET. This way we can keep
// the search query URL clean as a whistle:
// search/type/keyword+keyword
if (!isset($_POST['form_id'])) {
if ($type == '') {
// Note: search/node can not be a default tab because it would take on the
// path of its parent (search). It would prevent remembering keywords when
// switching tabs. This is why we drupal_goto to it from the parent instead.
drupal_goto('search/node');
}
$keys = search_get_keys();
// Only perform search if there is non-whitespace search term:
$results = '';
if (trim($keys)) {
// Log the search keys:
watchdog('search', '%keys (#type).', array('%keys' => $keys, '#type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
// Collect the search results:
// --- CHANGED ---
// $results = search_data($keys, $type);
// Instead of using search_data, use our own function
$results = test_search_data($keys, $type);
// --- END CHANGED ---
if ($results) {
$results = theme('box', t('Search results'), $results);
}
else {
$results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
}
}
// Construct the search form.
$output = drupal_get_form('search_form', NULL, $keys, $type);
$output .= $results;
return $output;
}
return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type);
}
// Note: identical to search_data() except for --- CHANGED ---
function test_search_data($keys = NULL, $type = 'node') {
if (isset($keys)) {
if (module_hook($type, 'search')) {
$results = module_invoke($type, 'search', 'search', $keys);
if (isset($results) && is_array($results) && count($results)) {
// --- CHANGED ---
// This dsm() is called immediately after hook_search() but before
// the results get themed. Put your code here.
dsm($results);
// --- END CHANGED ---
if (module_hook($type, 'search_page')) {
return module_invoke($type, 'search_page', $results);
}
else {
return theme('search_results', $results, $type);
}
}
}
}
}
You can use hook_search_page() to reorder or format the search result.
Hook search_execute allows you to modify the query in the way you needed. You can even fire new queries with custom sql, for example:
function mymodule_search_execute($keys = NULL, $conditions = NULL) {
// Do some query here.
$result = my_fancy_query();
// Results in a Drupal themed way for search.
$results[] = array(
'link' => (string) $result->U,
'title' => $title,
'snippet' => $snippet,
'keys' => check_plain($keys),
'extra' => array($extra),
'date' => NULL,
);

Resources