[TYPO3-english] TelephoneNumber ViewHelper
Robert Wildling
rowild at gmx.net
Sun Jun 30 18:14:18 CEST 2013
It seems as if I found a working way:
// Class
class Tx_Cbgooglemaps_ViewHelpers_Link_TelephoneNumberViewHelper extends
Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper
{
protected $tagName = 'a';
/**
* @param string $number The phone number to parse
* @param string $type The link type ("tel", "skype", "callme",...)
*
* @return string Rendered phone link
*/
public function render($number, $type = 'tel') {
$explodeStr = ",";
$content = "";
if (strpos($number, $explodeStr)) {
$i = 0;
$numberArray = explode($explodeStr, $number);
foreach ($numberArray as $num) {
$i++;
$content .= "<a href='" . $type . ":" .
self::clearPhoneNumber(trim($num)) . "'>" . trim($num) . "</a>";
if ($i < count($numberArray)) {
$content .= ", ";
}
}
} else {
$content .= "<a href='" . $type . ":" .
self::clearPhoneNumber(trim($number)) . "'>" . trim($number) . "</a>";
}
return $content;
}
/**
* This method strips unwanted characters from the given telephone number,
* such as white spaces or brackets
*
* @param string $number The number to be parsed
*
* @return string parsed
* @static
* @author Daniel Regelein <daniel.regelein at diehl-informatik.de>
*/
private static function clearPhoneNumber($number) {
$search = array('(', ')', '-', ' ', '/', '\\', '|', '{', '}', '[', ']');
return str_replace($search, '', $number);
}
}
// Template
{namespace l=Tx_Cbgooglemaps_ViewHelpers}
Telefon: <l:Link.TelephoneNumber number="{phone}" />
I just have my doubts, whether this is a good solution. Doesn't feel
very "Fluid".
I'd apprciate any comments on how to improve this ViewHelper!
Thanks! Robert
> Hi,
>
> I am trying to get into writing my own ViewHelpers. The first "bigger"
> test is a TelephonNumber ViewHelper that transforms phone numbers to
> links with the href:"tel:*" attr (knowing that modern devices do this
> automatically, also knowing about the respective meta-tag... as said:
> this is just for practicing).
>
> So far I got this, which works fine (thanks to the vhc collection; the
> whole thing gets implemented into the cbgooglemaps ext):
>
> // Script
> class Tx_Cbgooglemaps_ViewHelpers_Link_TelephoneNumberViewHelper extends
> Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
> protected $tagName = 'a';
> /**
> * @param string $number The phone number to parse
> * @param string $type The link type ("tel", "skype", "callme",...)
> * @return string Rendered phone link
> */
> public function render( $number, $type='tel' ) {
> $linkText = $number;
> $tagContent = $this->renderChildren();
> if ($tagContent !== NULL) { $linkText = $tagContent; }
> $this->tag->setContent($linkText);
> $this->tag->addAttribute('href', $type.':' . $number);
> return $this->tag->render();
> }
> }
>
> // Template
> {namespace l=Tx_Cbgooglemaps_ViewHelpers}
> Telefon: <l:Link.TelephoneNumber
> number="{phone}">{phone}</l:Link.TelephoneNumber>
>
>
> So far, so good. The problem starts, when there are is a list of comma
> seperated phone numbers to deal with. I started out with this:
>
> public function render( $number, $type='tel' ) {
> $explodeStr = ",";
> // if $number is an array...
> if( strpos( $number, explodeStr) ){
> $numberArray = trim( explode( explodeStr, $number ));
> foreach ( $numberArray as $num ) {
> // how to deal with the $this->tag->foo, when
> // there is only one tag?
> // how shall the return value be put together?
> }
> } else {
> // code as before
> }
> [...]
>
>
> As the comments already say, I do not know how to deal with the
> render->children() and the like commands. Also, the final render-command
> has to be placed outside the foreach loop, right? So what would be the
> proper way to build it?
>
>
> Thanks for your hints!
> Greetings, Robert
More information about the TYPO3-english
mailing list