Grayside.Org - api http://grayside.org/category/1/api en Programmatically Save a Block Configuration http://grayside.org/2013/01/programmatically-save-block-configuration <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even"><p>It looks like the Block <span class="caps">API</span> still leaves something to be desired: The only way to &#8220;properly&#8221; save the various region, title, and visibility settings of a block is using a form submission. This is a bit of a work-in-progress as far as documentation, but the function works as a simple wrapper of the form submit.</p> <div class="geshifilter"> <div class="php geshifilter-php" style="font-family:monospace;"> <pre style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009933; font-style: italic;">/** &nbsp;* Save a block configuration. &nbsp;* &nbsp;* @param array $block &nbsp;* &nbsp; - module: Required &nbsp;* &nbsp; - delta: Required &nbsp;* &nbsp; - visibility &nbsp;* &nbsp; - pages &nbsp;* &nbsp; - custom &nbsp;* &nbsp; - title &nbsp;* &nbsp; - roles &nbsp;* &nbsp; - regions &nbsp;* &nbsp; - pages &nbsp;* &nbsp; - status &nbsp;* &nbsp;* @return <span class="caps">NULL</span> &nbsp;*/</span> <span style="color: #000000; font-weight: bold;">function</span> extra_api_block_save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$block</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; module_load_include<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin.inc'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #000088;">$form_state</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'values'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$block</span><span style="color: #339933;">;</span> &nbsp; drupal_form_submit<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'block_admin_configure'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$form_state</span><span style="color: #339933;">,</span> <span style="color: #000088;">$block</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'module'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$block</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'delta'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div> </div> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-above"><div class="field-label">Terms:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/taxonomy/term/84">drupal7</a></div><div class="field-item odd"><a href="/category/1/blocks">blocks</a></div><div class="field-item even"><a href="/category/1/api">api</a></div></div></div> Sat, 12 Jan 2013 07:48:39 +0000 Grayside 101 at http://grayside.org http://grayside.org/2013/01/programmatically-save-block-configuration#comments Guzzle for Drupal: SDK Building http://grayside.org/2012/12/guzzle-drupal-sdk-building <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even"><p>For the last few months I&#8217;ve (intermittently) been thinking about how to build a nice <span class="caps">PHP</span> <span class="caps">SDK</span> for working with external APIs from <span class="caps">PHP</span> applications, and Drupal in general.</p> <p>This post is a sort of round up of my thoughts, hopefully I&#8217;ll find time to complete this project so I can post a detailed followup.</p> <h2>Selecting Guzzle</h2> <p>I&#8217;ve come to the conclusion (as has <a href="https://github.com/amazonwebservices/aws-sdk-for-php">Amazon</a>) that <a href="http://guzzlephp.org/">Guzzle</a> is the perfect <span class="caps">HTTP</span> Client framework to build such a thing in <span class="caps">PHP</span>. In the mean time, <a href="http://drupal.org/node/1862398">Drupal 8 has gotten fully onboard with the guzzled goodness</a>. So it&#8217;s looking increasingly like a go-to library.</p> <p>Decoupled architecture, careful obedience to the <span class="caps">HTTP</span> spec, lots of valuable (and optional) features, and a maintainer with a deep knowledge of cURL&#8217;s curlicues makes it a good system to lean on.</p> <h2>Basic Architecture</h2> <p>Guzzle has a very componentized architecture, and in recent months has separated out these components into separately downloadable packages, so you can use <a href="http://getcomposer.org">Composer</a> to get just the pieces you need.</p> <p>It also uses a Symphony-driven plugin architecture, which makes many different behaviors easily swappable. Check out the <a href="http://guzzlephp.org/guide/plugins.html">existing plugin library</a> and realize that most of them are ridiculously easy to override with your own version of the plugin, either implementing the interface or extending what&#8217;s there.</p> <h2>Laying Out the Client</h2> <ul> <li>Have a single &#8220;Client&#8221; class namespaced to the <span class="caps">API</span> you want to integrate.</li> <li>You can use the <a href="http://guzzlephp.org/guide/service/service_descriptions.html">Service Definition <span class="caps">DSL</span></a> to quickly get something working, but I question the performance of doing so. I have some crazy ideas about coupling a discover service with a Guzzle Mimetype to created an automatic client testing harness.</li> <li>You should probably have an abstract &#8220;Command&#8221; (<span class="caps">API</span> Resource) in place to impose any custom behaviors.</li> <li>You should probably group &#8220;categories&#8221; of resources together using a lower level abstract class or interface to impose still more. For example, I want to add some fault tolerance by making certain &#8220;change&#8221; operations enqueue-for-retry on failure.</li> </ul> <h2>Add the Drupal</h2> <p>Using the Adapter interfaces, it took me about 20 minutes apiece to put together Drupal caching and logging plugins. These will be brought in place with the proper <code>use</code> statements in a Drupal-specific Client class I will create as an extension of the generic <span class="caps">SDK</span> client.</p> <p>Similarly, I anticipate extending many of the Command classes with Drupal-specific variants to handle mapping (data conversion) from Drupal entities to the <span class="caps">API</span> payload&nbsp;structure.</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-above"><div class="field-label">Terms:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/category/1/guzzle">guzzle</a></div><div class="field-item odd"><a href="/category/1/composer">composer</a></div><div class="field-item even"><a href="/category/1/sdk">sdk</a></div><div class="field-item odd"><a href="/category/1/api">api</a></div><div class="field-item even"><a href="/taxonomy/term/1">drupal</a></div></div></div> Wed, 12 Dec 2012 05:42:44 +0000 Grayside 100 at http://grayside.org http://grayside.org/2012/12/guzzle-drupal-sdk-building#comments