18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11<br />

In some cases you may not need some of this functionality provided by the interface.<br />

For instance, in our example, we are only reading photos from Twitpic without<br />

offering the ability to write data anywhere, so functions like stream_write()<br />

and stream_mkdir() don't apply. In these cases we simply return FALSE.<br />

For the full implementation details of<br />

<strong>Drupal</strong>StreamWrapperInterface, refer to http://api.drupal.<br />

org/api/drupal/includes--stream_wrappers.inc/7. You may<br />

also want to refer to PHP's prototype stream wrapper class at http://<br />

php.net/manual/en/streamwrapper.stream-open.php.<br />

<strong>Drupal</strong>StreamWrapperInterface is quite extensive, with over 20 functions to be<br />

defined by the implementing classes. Every stream wrapper should implement each<br />

of PHP's file handling functions. As mentioned earlier, many of these simply return<br />

FALSE. Others simply pass through to matching PHP functionality. (stream_eof()<br />

simply calls and returns the results of feof($handle).)<br />

Since the class is so large, you may want to put it into a separate file to improve<br />

readability and maintainability of your code. You can do this by creating a new file<br />

in your module's directory, and adding it to the files[] array in your module.info<br />

file as shown:<br />

files[] = twitpicstreamwrapper.inc<br />

In order to keep things simple, we will only discuss the most noteworthy parts of the<br />

class shown in the following code. The full code listing can be downloaded from the<br />

Packt website.<br />

/**<br />

* Twitpic Stream Wrapper<br />

*<br />

* This class provides a complete stream wrapper implementation.<br />

*/<br />

class TwitPicStreamWrapper implements <strong>Drupal</strong>StreamWrapperInterface {<br />

/**<br />

* Instance URI as scheme://target.<br />

*/<br />

protected $uri;<br />

/**<br />

* A generic resource handle.<br />

*/<br />

public $handle = NULL;<br />

[ 321 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!