We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin Help Request: Using Format Service in EE3

Development and Programming

DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

I’ve come across a situation where I would like to convert text with accents text to text without accents. I looked around for an add-on that might do this but I wasn’t able to find one. I’ve never built a plugin before, but this seemed like it might be a pretty simple one to get my feet wet.

I poked around in the documentation and found the Accented Characters to Ascii formatter, which appeared to be just what I was looking for.

Sadly, after spending some time in the documentation (I’m still on EE3 for this site), I still can’t seem to figure this out. I’m sure I’m missing something super basic, so I thought I’d post here to see if anyone could help me get this across the finish line….

I’ve got a pi.convert_accents.php plugin with the following code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Convert_accents
{
 public $return_data = "";
 public function __construct($str = NULL)
 {
  if (empty($str))
  {
         $str = ee()->TMPL->tagdata;
  }
  $str = ee('Format')->make('Text', $str)->accentsToAscii();
  $this->return_data = $str;
 }
}
/* End of file pi.convert_accents.php */

When I try to use it in my template, I get an error:

Fatal error: Uncaught Error: Call to undefined method EllisLab\ExpressionEngine\Service\Formatter\Formats\Text::accentsToAscii()

I’m assuming that I’m not using the formatter properly, but I can’t seem to find anything in the documentation that might help me fix it.

Any suggestions?

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Welcome DSite! You’re very close, good job on getting your first plugin going! However, that formatter isn’t available in v3. Please check the v3 docs for its limited available formatters. Expansive modifiers and formatters are one of the new features of v4. Is there a reason you cannot upgrade?

       
DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

Hi Derek,

Thanks for your response. I thought that I had selected the v3 docs but of course you’re right. I guess it’s good to know that I have a solution prepared once I get ready to upgrade. (I’d guess around 80% of the site is EE4 compatible, but I’m still relying on a few add-ons that haven’t indicated compatibility and I just haven’t had the time or budget to do an upgrade test and figure out workarounds.)

So… assuming I’m working with EE3 for now, is there an equivalent formatter that I could use? I see that there is a legacy text helper called convert_accented_characters($str), but replacing the formatter in my previous code with

$str = convert_accented_characters($str);

gives me a similar Call to undefined method error.

Am I applying the legacy text helper incorrectly? Is there something in between the legacy helper and the v4 formatter that would do the trick? I’ve poked around in the v3 docs and I can’t find something that is clear enough to me.

Thanks for your help.

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

You are using the function correctly, but it’s not loaded by default. You should be able to use that if you first add:

ee()->load->helper('text');
I’m still relying on a few add-ons that haven’t indicated compatibility and I just haven’t had the time or budget to do an upgrade test and figure out workarounds

Do you have a list? Most add-ons don’t need anything if they are already v3 compatible. The exception are add-ons that work with statuses or manipulate channel field data.

       
DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

Okay, I’m getting closer. I no longer get the undefined method error, but the output is not at all what I expected.

Here is my plugin:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Convert_accents
{
 public $return_data = "";

 public function __construct($str = NULL)
 {
  ee()->load->helper('text');
  
  if (empty($str))
  {
         $str = ee()->TMPL->tagdata;
  }

  $str = convert_accented_characters($str);

  $this->return_data = $str;
 }
}

/* End of file pi.convert_accents.php */

And here is my basic template:

<ul>
{exp:channel:entries channel="brands"}
    <li>{title} - {exp:convert_accents}{title}{/exp:convert_accents}</li>
{/exp:channel:entries}
</ul>

The title tag without the plugin outputs just fine. The title tag with the plugin outputs just the second letter in the title. Like this:

  • This is the Title - h
  • Another Title - n
  • Accented Títle - c

That’s obviously not what I expected. Is there something obvious that I’m doing wrong?

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Yes, my apologies, it’s been quite a long time since I used that helper method. It expects a character at a time, so you should be able to:

$str = utf8_decode($str);
$str = preg_replace_callback('/(.)/', 'convert_accented_characters', $str);
       
DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

Ooh! That looks like it worked! Thanks so much.

There’s one character that didn’t come through properly, but that might be a different text helper. Some of the titles have a smart apostrophe (like ’) . That is getting converted to a question mark:

  • This isn’t working - This isn?t working

I’m guessing that has something to do with the utf8_decode. I’d absolutely be okay with converting that to a straight quote for this purpose. Is there another text helper that I could slip in there? If not, I could probably use Streeng or Low Replace or something like that to make the swap before applying my plugin, but it would obviously be cleaner if I could find a way in the plugin itself.

Thanks again for helping me get this far.

       
DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

Also, since you asked… I think that the add-ons that aren’t explicitly EE4 compatible include Mo Variables, Resource Router and Zenbu. (We’re also using a custom module that I’m guessing might be the biggest roadblock at the moment.)

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

Forgot to follow up on this, sorry. Both Mo Variables and Resource Router should work on v4 without any modifications. Zenbu isn’t, but IIRC that won’t break any functionality on your site to uninstall, it just modifies the Entry Manager page right?

       
DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

Thanks again for your help with this, Derek. I decided to let the plugin focus on the accents and I’m using Streeng to convert the quotes. Everything is finally working smoothly!

Good to know about those add-ons. If I have some time before our soft launch, maybe I’ll spin off another dev site to do a test upgrade.

Have a nice week!

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones

My guess is that those curlies aren’t unicode, are they pasted from Word? I think it may add some invisible control characters that would trip utf8_decode up. You could also try commenting that line out, and not need two separate plugins.

       
DSite's avatar
DSite
16 posts
7 years ago
DSite's avatar DSite

They’re curlies typed straight into the text input field (like shift-option-] on the Mac), so I’m not sure what the issue is.

I poked around a bit more and found str_replace, which allowed me to bring the quote conversion into my plugin. And because this is as much a learning experience as anything, I decided to make the quote conversion function a parameter. Does this look like it’s the proper construction? It works, which is the most important thing, but I also want to make sure I’m doing it right.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Convert_accents
{
     public $return_data = "";
     public function __construct($str = NULL)
     {
          ee()->load->helper('text');
          if (empty($str))
          {
               $str = ee()->TMPL->tagdata;
          }
          $parameter = ee()->TMPL->fetch_param('convert_quotes');
          switch ($parameter)
          {
               case "yes":
                    // Converts left and right single and double quotation marks from "smart" to straight
                    $str = str_replace("’", "'", $str);
                    $str = str_replace("‘", "'", $str);
                    $str = str_replace('“', '"', $str);
                    $str = str_replace('”', '"', $str);
                    break;
               case "no":
                    break;
          }

          // Converts accented characters to unaccented characters
          $str = utf8_decode($str);
          $str = preg_replace_callback('/(.)/', 'convert_accented_characters', $str);

          $this->return_data = $str;
     }
}
/* End of file pi.convert_accents.php */

I can’t tell if the “no” option is a proper convention, even if it would never be used.

As always, I greatly appreciate your time. I know you’re busy making EE4 even better, and there are probably a million things that would be a better use of your time than remedial plugin coaching. But it’s been helpful.

       
Derek Jones's avatar
Derek Jones
7,561 posts
7 years ago
Derek Jones's avatar Derek Jones
$convert_quotes = get_bool_from_string(ee()->TMPL->fetch_param('convert_quotes', 'yes'));

The above will fetch the parameter, supply a default value of yes if the parameter doesn’t exist, and get_bool_from_string() will give you a PHP boolean value so you don’t have to worry about yes vs. YeS vs. 1 vs. y vs. on etc.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.