drupal 7 http://andrejgaluf.com/tags/drupal-7 en Drupal 7: Setting title module field programmatically http://andrejgaluf.com/blog/2015-11-27/drupal-7-setting-title-module-field-programmatically <div data-history-node-id="10" class="node node--type-blog-post node--view-mode-rss ds-1col clearfix"> <div class="field field--name-dynamic-token-fieldnode-custom-submitted field--type-ds field--label-hidden field__item"><p>Submitted by <a href="http://andrejgaluf.com/users/admin">Andrej Galuf</a> on 27. November 2015.</p> </div> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Hello and welcome back to Drupal Tips and Tricks. Today we will be looking at the <a href="https://www.drupal.org/project/title">Title</a> module in combination with <a href="https://www.drupal.org/project/entity_translation">Entity Translation</a> and we will be trying to change the value of the title field programmatically.</p> <p>Title module replaces the title property on an entity with a field that can hold different language values. Unfortunately, this isn't as simple as it sounds, because the core title property is defined and used by the core directly in the node table.</p> <p>As a result, there are some unforseen side effects, such as problems when saving the value outside the dedicated code, for instance programmatically or from a custom form submit.</p> <p>Let's look at one such example submit:</p> <pre> <code class="language-php">&lt;?php $wrapper = entity_metadata_wrapper('node', $nid); $wrapper-&gt;language('en')-&gt;title_field-&gt;set('Test'); $wrapper-&gt;save();</code></pre> <p>Unfortunately, the above code does not work as expected. If you're lucky, this code won't do anything - the title value won't change. If you're unlucky, the core will break your titles completely (for instance, set a title in original language rather than translation).</p> <p>How can we fix this?</p> <p>First, you'll need the <a href="https://www.drupal.org/node/2267251">patch #27 here</a>. Copy it to the title module folder and apply as usual (patch -p1 &lt; 2267251-27.patch). Now, let's try again:</p> <pre> <code class="language-php">&lt;?php // Node language is 'en' $wrapper = entity_metadata_wrapper('node', $nid); $wrapper-&gt;language('en')-&gt;title_field-&gt;set('English Title'); $wrapper-&gt;language('de')-&gt;title_field-&gt;set('German Title'); $wrapper-&gt;save();</code></pre> <p>Can you guess what this code will do? It will save the german title, but not the english one.</p> <p>... what?</p> <p>Title module makes sure that the title_field in original language and node title are syncronized at all times. Unfortunately, the code ignores the value in the original language when set through the title_field. Here's the correct way to save the original and translated title value:</p> <pre> <code class="language-php">&lt;?php // Node language is 'en' $wrapper = entity_metadata_wrapper('node', $nid); $wrapper-&gt;title-&gt;set('English Title'); $wrapper-&gt;language('de')-&gt;title_field-&gt;set('German Title'); $wrapper-&gt;save();</code></pre> <p>Hopefully, this will save you some debugging time.</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tags/quick-tips" hreflang="en">quick tips</a></div> <div class="field__item"><a href="/tags/drupal" hreflang="en">drupal</a></div> <div class="field__item"><a href="/tags/drupal-7" hreflang="en">drupal 7</a></div> </div> </div> <div class="field field--name-dynamic-token-fieldnode-comments-title field--type-ds field--label-hidden field__item"><span>Comments</span> </div> <section class="field field--name-comment field--type-comment field--label-hidden comment-wrapper"> <a id="comment-37"></a> <article data-comment-user-id="0" about="/comment/37" typeof="schema:Comment" class="comment js-comment by-anonymous"> <mark class="hidden" data-comment-timestamp="1485641907"></mark> <footer class="comment__meta"> <article typeof="schema:Person" about="/user/0" class="profile"> </article> <p class="comment__submitted"><span rel="schema:author">Submitted by <span lang="" typeof="schema:Person" property="schema:name" datatype="">d70rr3s (not verified)</span> on Thu, 01/26/2017 - 10:41</span> <span property="schema:dateCreated" content="2017-01-26T09:41:35+00:00" class="rdf-meta hidden"></span> </p> <a href="/comment/37#comment-37" hreflang="en">Permalink</a> </footer> <div class="content"> <h3 property="schema:name" datatype=""><a href="/comment/37#comment-37" class="permalink" rel="bookmark" hreflang="en">You save the day</a></h3> <div property="schema:text" class="clearfix text-formatted field field--name-comment-body field--type-text-long field--label-hidden field__item"><p>Thanks a lot! I was driving mad with this issue. I have a custom web service for pushing nodes to my site and thus I was receiving and set in right (well I thought was the right way to do it) on save the title_field value was set back to null. Anyway thanks again and great tip, should be included on Title module docs.</p> </div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=37&amp;1=default&amp;2=en&amp;3=" token="aa5h27Zf_Io7eS7_oKymx0W8fnjVh-hWQNuWQ3Ciw78"></drupal-render-placeholder> </div> </article> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=10&amp;2=comment&amp;3=comment" token="YckVX30_aqr-xobBuxYh1_EWuyROHe8PdCJuREjuTIY"></drupal-render-placeholder> </section> </div> Fri, 27 Nov 2015 17:39:30 +0000 admin 10 at http://andrejgaluf.com