The following example shows you how to enhance the socialbookmark library delivered with each release to meet your demands.
The socialbookmark library contained in each apf-codepack-* release are intended to be included in the content area of your application via XML tag. For this reason, the module includes a taglib, whose characteristics are described on the Socialbookmarking page. The tag definition allows you to specify a page title within the tag definition, that is used as a title when bookmarking the current page. This definition is statically, because it is specified on tag definition time and not on execution time. This is problematically with CMS webpages, because there, titles must be dynamic!
class SocialBookmarkBarTag extends Document {
public function __construct(){
$this->attributes['width'] = '20';
$this->attributes['height'] = '20';
$this->attributes['title'] = null;
$this->attributes['url'] = null;
$this->attributes['target'] = null;
}
public function transform(){
// get bookmark manager
$sBM = &$this->getServiceObject('modules::socialbookmark::biz','SocialBookmarkBarManager');
// configure width and height
$sBM->set('Width',$this->attributes['width']);
$sBM->set('Height',$this->attributes['height']);
// konfigure URL parameters
if($this->attributes['url'] != null){
$sBM->set('URL',$this->attributes['url']);
}
if($this->attributes['title'] != null){
$sBM->set('Title',$this->attributes['title']);
}
if($this->attributes['target'] != null){
$sBM->set('Target',$this->attributes['target']);
}
// return bookmark HTML code
return $sBM->getBookmarkCode();
}
}In order to make the title passed to the bookmark service dynamic, ist is well to define another tag library, that only contains the difference of functionality, but uses the existing library. To make it easier to show the proceeding to you, I define, that the tag should be named my:bookmark. The taglib then contains the following source code:
import('modules::socialbookmark::pres::taglib', 'SocialBookmarkBarTag');
class MyBookmarkTag extends SocialBookmarkBarTag {
public function transform(){
// gather the title of the current page
$title = /* current title */;
// fill the title attribute, that is used by the parent class
$this->setAttributes('title', $title);
// generate output with aid of the parent class
return parent::transform();
}
}As you can see in the code box above, the transform() only contains the gathering of the page's title. There, the title is assigned to the attributes offset used by the parent class. The core function is still done by the SocialBookmarkBarTag class. Because of the fact, that our wrapper class inherits from SocialBookmarkBarTag, it is still possible to use the
attributes within the tag definition. The following code shows you how to include the new taglib into your templates:
<core:addtaglib
namespace="your::namespace"
class="MyBookmarkTag"
prefix="my"
name="bookmark"
/>
<my:bookmark width="16" height="16" />Details on tag implementation can be found under Implementation of tags.
In case of API changes to the third-party library, adaptation to the new API is limited to filling the attribute title, because the call of the parent transform() is still the same. A second advantage is, that the API of the wrapper class MyBookmarkTag remains the same and the components using the API of the wrapper class must not be changed. The example described in the present tutorial only contained the enhancement of taglibs, but this principle can be adopted to any other domain.
JetBRAINS supports the development of the APF with PHPStorm licenses and we feel confidential that PHPStorm strongly influences the APF's quality. Use PHPStorm!
Proud to useIntelligent PHP IDE for coding, testing and debugging with pleasure