Jump to content

Retrieve The Type of A Field From Inside A Module


Recommended Posts

Ah sorry for the misinforming, ok it's the value is returned by the formatValue so you grab it via $event->return.

This should work:

wire()->addHookAfter("FieldtypeText::formatValue", null, function($event){
    $value = $event->return;
    $value = str_replace(array("<p>","</p>"), "", $value);
    $event->return = $value;
});
  • Like 1
Link to comment
Share on other sites

Hi, 

I only today realized that the following hook is not working fully as intended:

 wire()->addHookAfter("FieldtypeTextLanguage::formatValue", null, function($event){    $value = $event->return;
    $value = str_replace(array("<p>","</p>"), "", $value);
    $event->return = $value;
});
 
It also gets called on FieldtypeTextareaLanguage (I am only using FieldtypeTextLanguage and FieldtypeTextareaLanguage). I verified by adding:
echo $event->arguments("field")->type;
 
inside the hook function. 
 
Does anyone know why this happens?
 
Cheers, 
 
Stefan
 
EDIT: Removed a typo:
 (I am only using FieldtypeText and FieldtypeTextarea)  changed to: (I am only using FieldtypeTextLanguage and FieldtypeTextareaLanguage)
Link to comment
Share on other sites

I'm not sure I fully understand, it doesn't get called also on FieldtypeTextareaLanguage. And how do you now if you say you only use FieldtypeText and FieldtypeTextarea?

One thing I could imagine is that FieldtypeText is base fieldtype for a lot of the other text fieldtypes, but testing here it works fine and only is replacing for the FieldtypeTextLanguage.

Link to comment
Share on other sites

@soma

I am sorry, there was a typo in my previous post. What I wanted to say is that I am only using FieldtypeTextLanguage and FieldtypeTextareaLanguage at the moment. When I echo the type of the field the hook is called on, I get both FieldtypeTextLanguage and FieldtypeTextareaLanguage as output. 

You are right about the fieldtypes:

FieldtypeTextareaLanguage extends FieldtypeTextarea

FieldtypeTextarea extends FieldtypeText

Also, I actually made two hooks to cover both FieldtypeTextLanguage, and FieldtypeText (although I am not using FieldtypeText at this moment, but will later):

$fieldClasses = array("FieldtypeTextLanguage", "FieldtypeText");
foreach ($fieldClasses as $class) {

    wire()->addHookAfter("$class::formatValue", null, function(HookEvent $event){

       $value = $event->return;
       $value = str_replace(array("<p>","</p>"), "", $value);
       $event->return = $value;
    }); 
}

Cheers, 

Stefan

Link to comment
Share on other sites

@bytesource

I know Soma has a perfectly good solution to this (adding the Paragraph stripper) but I'd like to support single-fields directly in textile so I have just checked code into the master branch to add this ability. I hope to do an update to the textileTextformatter soon that will let you get at this directly without having to strip anything using a second formatter.

Link to comment
Share on other sites

@netcarver

I noticed that the array on line 35 of TextformatterTextile.module was not closed.

After fixing the typo and updating the modules in the module section of the admin, I wanted to install Textile Field. However, the install button was diabled as can be seen on the attached screenshot. 

Do I need to first uninstall the old Textile module and then reinstall the updated one from within the admin? What I did was replacing the old folder with the new one before hitting 'Check For New Modules'. I was afraid that reinstalling the old version first might de-attach the Textile formatter from its respective fields. 

Cheers, 

Stefan

Link to comment
Share on other sites

@bytesource

Thanks for that. I've fixed it and pushed it up to github.

Regarding installing the new version. I'm not sure about the 'official' way to go about this but I think Ryan will have orphan checks built in to the code so you can't just un-install things that are in use. As the new version still provides exactly the same formatters as before I'd try backing up the existing folder (somewhere out of your directory tree) then dropping the new module in its place and then hitting the 'Check For New Modules' button and seeing where it gets you. Otherwise, switch the old folder back in, remove the formatter from your fields, deinstall, switch out etc.

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...