[TYPO3-english] TelephoneNumber ViewHelper

Robert Wildling rowild at gmx.net
Sun Jun 30 13:31:50 CEST 2013


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