[TYPO3-english] TelephoneNumber ViewHelper

Robert Wildling rowild at gmx.net
Mon Jul 1 21:19:26 CEST 2013


Hi, Henjo,

thanks for your response!

Would your solution work? The (only) input field in the BE allows for 
one or more (comma seperated) numbers, like this:

01 3423422342, 023 234234234, 0234 23423423423

But what if there is only one number? Would Fluid ignore the other 
arguments?

Also, concerning escape of vertain unwanted characters: where would I 
have to do that?

Well... newbie I am! :-)

Thanks! Robert


> Hi Robert,
>
> why don't just parse the phonenumbers as an array in Fluid?
> <l:link.telephoneNumber numbers="{0:'firstnumber', 1:'secondnumber',
> 2:'...'}" type="tel" />
>
>
>
> Kind regards,
>
> Henjo
>
> Problems are small because we learned how to deal with them.
> Problems are big because we need to learn how to deal with them.
>
>
> On Mon, Jul 1, 2013 at 8:56 PM, Robert Wildling <rowild at gmx.net> wrote:
>
>> Anybody, please? Some directions?
>>
>>
>>
>>   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<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
>>>>
>>>
>>>
>> ______________________________**_________________
>> TYPO3-english mailing list
>> TYPO3-english at lists.typo3.org
>> http://lists.typo3.org/cgi-**bin/mailman/listinfo/typo3-**english<http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-english>
>>



More information about the TYPO3-english mailing list