Jump to content

Recommended Posts

Posted

Hello

quite strange request!
I've got two fields (inside a Profields: Textarea and Profields: Table) filled with generated URL. I would lock them but would also like to have the link instead of the flat string. Any idea?

Posted
// site/ready.php
$wire->addHookAfter("Inputfield::render", function($event) {
  $field = $event->object;
  if($field->name !== "yourfield") return;
  $event->return = "<a href='foo/bar'>Your link</a>";
});

You could use one of the Runtime Markup Inputfields or something like the hook above (untested - just an example to get you started).

  • Like 1
Posted
41 minutes ago, bernhard said:

// site/ready.php
$wire->addHookAfter("Inputfield::render", function($event) {
  $field = $event->object;
  if($field->name !== "yourfield") return;
  $event->return = "<a href='foo/bar'>Your link</a>";
});

You could use one of the Runtime Markup Inputfields or something like the hook above (untested - just an example to get you started).

Hi @bernhard and thanks. This would work perfectly in a textfield but unfortunately I have these fields inside a Profields: Textareas and Profields: Table. How can I hook them?

Posted

From the looks of the source, at least Profields table doesn't seem to be very hookable at that detail (it's creating the markup concatenating strings), you could only hook on the inputfield render method and that includes the whole markup for the table field. I'd try with some custom javascript.

Textareas does seem to trigger the Inputfield render method, so I'd give it a shot with @bernhard's hook. 

Posted

This is an example of something I do with Profields Table for replacing Twilio message and error codes with links to the entry in the logs on the Twilio site. You should be able to adapt to your needs.

// link SID and error code columns in User message tables to the Twilio log entry
$this->wire()->addHookAfter('InputfieldTable::render', function($event) {

    if(!in_array($event->object->name, array('received_messages', 'incoming_messages', 'queued_messages', 'undelivered_messages', 'failed_messages'))) return;

    $text = $event->return;
    if(preg_match_all("/<td>(S|M)M(.*?)<\/td>/", $text, $matches)) {
        foreach($matches[0] as $match) {
            $sid = str_replace(array('<td>', '</td>'), '', $match);
            $text = str_replace($match, '<td><a href="https://www.twilio.com/console/sms/logs/'.$sid.'">'.$sid.'</a></td>', $text);
        }
    }
    if(preg_match_all("/<td>([0-9]{1,})<\/td>/", $text, $matches)) {
        foreach($matches[0] as $match) {
            $errCode = str_replace(array('<td>', '</td>'), '', $match);
            $text = str_replace($match, '<td><a href="https://www.twilio.com/docs/api/errors/'.$errCode.'">'.$errCode.'</a></td>', $text);
        }
    }
    $event->return = $text;

});


 

  • Like 5
Posted
21 hours ago, nabo said:

This would work perfectly in a textfield but unfortunately I have these fields inside a Profields: Textareas and Profields: Table.

Sorry, obviously I missed that info ? I don't know how complex your field output is, but you could nonetheless use my hook and recreate the complete output. Maybe that could even improve your UI.. Just to throw in an idea without knowing your scenario.

  • 2 weeks later...
Posted

@nabo - I just did some cleanup of that code and was actually also needing to link URLs, so I just use this:

        $text = preg_replace('/(<td>)(http(.*?))(<\/td>)/', '$1<a href="$2">$2</a>$4', $text);

which is obviously much cleaner than the preg_match_all approach - I think I must have been in a rush last time and not thinking properly ?

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
  • Recently Browsing   0 members

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