Jump to content

How to use TextformatterMarkdownExtra class


TomPich
 Share

Recommended Posts

Hello there,

Back again for a new question.

So I began a website for a customer and came upon a small problem that I can’t solve. I have a textarea field for which I don’t want the end user to be able to enter title or list, so I don’t want to use TinyMCE. It is supposed to be a simple paragraph. I decided to allow <strong> and <em>, so I thought using MarkDown would be a possible solution.

I installed and used the TextformatterMarkdownExtra module, and it works fine, but VS code keeps reporting an error because it’s not able to find the class definition. Here is the code in the template :

$md_formater = new TextformatterMarkdownExtra;
$content = $md_formater->markdown($hero->content);

When I implement a new instance of TextformatterMarkdownExtra (first line), VS code underscores it in red, saying that the class is not found. It can’t import it, neither. Apparently, in the final result, Processwire sorts it out because it works perfectly. But the error reporting is annoying because it may distract me from real errors.

Am I doing something wrong?

Many thanks in advance.

TomPich

Link to comment
Share on other sites

Thank you for your answer.

It’s already the case. The class is in a .module file, maybe it is due to that?

––––

Okay, so I discovered that you can not import a class just like normal php application.

This worked :

$md_formater = $modules->get("TextformatterMarkdownExtra");
$content = $md_formater->format($her->content);

I wonder, however, if there is a way to get the result directly with the input config, instead of doing it programmatically...

Edited by TomPich
Solution found
Link to comment
Share on other sites

  On 10/13/2023 at 4:55 PM, TomPich said:

This worked :

Expand  

Ho yes, tthat's the good way to get module instance. ?
But the original issue about editor isn't solved, for example if you want completion on this module by adding a php comment it won't work.
 

/* @var TextformatterMarkdownExtra $md_formatter */

Maybe VSCode PHP extension is not parsing .module files by default, and maybe that's configurable?
I used VSCode for a few years but use PHPStorm now and will never go back. ^^

  On 10/13/2023 at 4:55 PM, TomPich said:

I wonder, however, if there is a way to get the result directly with the input config, instead of doing it programmatically...

Expand  

In PW admin, in your field configuration > Details > Formatter, select the formatter for markdown.
Then you just use $page->field to get the formatted value.

Link to comment
Share on other sites

Hello da²,

When I have time, I’ll look into this VS code config settings. PHPstrom is not free, so for now I’ll stick to VS code...

Thanks for the tip about Formatter. It works perfectly. Processwire is really a jewel of smartness and simplicity.
I just love it, and I’m happy to use a CMS again. It’s not something I have to fight against any more... ?

And it’s very pleasant to know that there is a reactive and friendly community.

Have a good day.

Link to comment
Share on other sites

You can configure VS Code to understand "*.module" files as PHP.

Go to "Settings" and then "Text Editor > Files". Under "Associations" press "Add" button and add "*.module" (without the quotes) under Item/key and "php" under Value. Hit "Ok".  Might need to restart VS Code for it to take effect.

Also, you need a double-asterisk at the front of the comment to get the code hints (and also make sure the variable name is correct/matches!):

/** @var TextformatterMarkdownExtra $md_formatter */
$md_formatter = $modules->get("TextformatterMarkdownExtra");
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...