Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Bulk insert for links
I’m working on a project where we need to add references to articles. Theses references are links and each articles has a lot of them.
It’s too tedious for non professional to add links one by one.
To help with taht, I would like to offer a way to do a bulk insert of links right in the write panel.
I think of a textarea input where a writer would paste a csv formatted text, with a submit button to trigger a plugin that would do the bulk insert in the links table.
Moreover, inside the plugin I would need to get back the ids of the rows inserted.
It would permit to append a prepared <txp:linklist> to the body textarea.
I’ve been away from Textpattern plugin dev for too long a time now, I would definitively use some help from you gurus ;)
What would be a sensible way to do what I need ?
Thanks for your help :)
Offline
Re: Bulk insert for links
Great to see you back.
It’s definitely doable. Implementation will depend on what you want to do if it finds links that are already defined: update them? Delete and reinsert? And does the same URL alone constitute a clash?
Before you reinvent any wheels, though, check out etc_post which is the definitive swiss army knife for inserting data into Txp from the front end.
There’s probably a way to harness that via a form, paste CSV data into a text area and have the plugin iterate over it (or wrap the plugin calls up in a couple of lines of code to create a loop and post the data that way). Not sure if it returns the inserted IDs though.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Bulk insert for links
Do you plan on doing any link management through Textpattern? Or are do the links have descriptive texts? If not, maybe they don’t need to go into the Links panel at all.
Another option if each article basically just has a straight list of links at the end would simply be to use glz_custom_fields to add a textarea custom field to your article and then paste in a list of textiled links into that.
If you already have the links in a spreadsheet, you can probably use a simple text concatenation formula in excel / numbers / sheets & co and create a spreadsheet column with "Linktext":link-url
. You can then copy and paste the column straight from the spreadsheet to the textarea in Textpattern, without the detour of a csv file.
If you need it to be a bullet or ordered list and you don’t fancy preformatting the list in textile, you should also be able to process the html output using the trim
and replace
attributes to search and replace the <p>
and </p>
of the custom field output to <li>
and </li>@ and then wrap that with your own <ul class=“reference-list”> in your page template.
There’s also the new-ish breakby
and breakform
attributes which you can use to break apart a string or textpattern output at certain points (e.g. tags) and then pass the item through a textpattern form. It just depends on the kind of output you get.
EDIT: I tried it out on the demo site:
Say you enter a straight list of textiled links into a textarea like this (I used the excerpt field for ease of testing in the demo):
"Textpattern Site Showcase":https://textpattern.com/showcase/
"Textpattern Brand and Voice":https://docs.textpattern.com/brand/
"Textpattern Tips":https://textpattern.tips/
"TXP Magazine":https://txpmag.com/
"Textile Markup Language Documentation":https://textile-lang.com/
Then you can transform that into a list by doing:
<txp:variable name="list" value='<txp:excerpt escape="p" />' breakby="<br>" break="li" wraptag="ul" class="reference-list" output />
The escape="p"
only removes the wrapping <p>
tag that txp:excerpt automatically adds when textiling the content. The breakby="<br>"
seemed to need the angle brackets (otherwise they show up in the output) and the rest is straightforward wraptag
and break
formatting. That gives you:
<ul class="reference-list">
<li><a href="https://textpattern.com/showcase/">Textpattern Site Showcase</a></li>
<li><a href="https://docs.textpattern.com/brand/">Textpattern Brand and Voice</a></li>
<li><a href="https://textpattern.tips/">Textpattern Tips</a></li>
<li><a href="https://txpmag.com/"><span class="caps">TXP</span> Magazine</a></li>
<li><a href="https://textile-lang.com/">Textile Markup Language Documentation</a></li>
</ul>
TXP Builders – finely-crafted code, design and txp
Offline
Re: Bulk insert for links
jakob wrote #337609:
Do you plan on doing any link management through Textpattern? Or are do the links have descriptive texts? If not, maybe they don’t need to go into the Links panel at all.
Yes, we need the Links panel. These link will also be published in special page with filters.
Bloke wrote #337607:
Great to see you back.
It’s definitely doable. Implementation will depend on what you want to do if it finds links that are already defined: update them? Delete and reinsert? And does the same URL alone constitute a clash?
Let’s say that at first approximation, links will always be unique :D
Wrt to etc_post, if I understand correctly, it’s meant for data entry on the public side. This is not what we want. Writers will create their article in the write panel. I want to make their life easier by not requiring them to key in dozen of links in the Links panel
I don’t mind writing a plugin, if it’s what it take.
I understand that adding the textarea is doable with pluggable_ui(‘article_ui’, ‘body’), probably when the article is already created.
In that case could I use ‘article_saved’ callback? But will the content of the textarea be available in the callback ?
Offline
Re: Bulk insert for links
@Jakob, I just read your edit, that’s good to know!
For now, the project need to have the links available for other pages.
I’ll keep your example for future reference.
Offline
Re: Bulk insert for links
Ack, I missed “bulk insert of links right in the write panel” bit. Sorry.
In which case, yes, pluggable_ui to add a text area will work just fine.
Hook your custom save routine into the publish
and save
steps (pre="1"
) so you can read the $_POST content via ps('your_new_fieldname')
.
Then parse it into lines/columns, sanitize each piece, and call safe_insert()
to stick them in the txp_link table. And clear your variable from the POST array before Txp gets hold of it.
Two main ways to associate the links with the article. One is to get the IDs of the inserted rows, join them by comma and add them to the POSTed article content as a nominated custom field. The other is to put the article’s ID in each link’s row. You might be able to use the linksort
column for this although I don’t think you can easily extract them via a tag to display the links on the website when the article is fetched.
Alternatively, a dedicated link custom field could be used. Jakob probably has a plugin for this (he definitely has one for file and image custom fields).
As well as grabbing the relevant links on the website that match the article ID, you’d also need another function in your plugin to grab the list of link IDs, fetch the relevant links and display them in your textarea when people revisit the Write panel to edit an article.
That is where the fun begins if they edit any links in the textarea as you need to know which to replace and which to insert. If it was me doing this, I’d just delete all the links associated with the article when they hit save/publish and recreate them from whatever is in the textarea.
Last edited by Bloke (2024-08-14 20:10:38)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Bulk insert for links
Lovely! This answer a lot of my questions.
As to associate the link with the article I’ll go with the custom_field holding the ids. It should work with <txp:linklist> if I go this route.
Regarding the option to edit the links after the initial load, I would simplify with not showing the textarea if links ids exists and add a link to the Link panel if edit is needed.
Offline
Re: Bulk insert for links
planeth wrote #337619:
As to associate the link with the article I’ll go with the custom_field holding the ids. It should work with <txp:linklist> if I go this route.
Ooh yeah. That would work nicely.
Regarding the option to edit the links after the initial load, I would simplify with not showing the textarea if links ids exists and add a link to the Link panel if edit is needed.
In which case you only need to hook into the publish
step to handle saving, and the edit
step to simply add the link to the Links panel. That’s considerably simpler, but how would you resync the (edited, possibly deleted and possibly additional) list of link IDs into the custom field of the article they came from?
Last edited by Bloke (2024-08-14 21:37:25)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Bulk insert for links
Would using a link category named after the article (refs_{article_id}
, a shortened article_url_title or some other unique identifier) work as a way of associating links with an article? That would make any possible later additions or removals relatively straightforward without having to extract or add to a lists of comma-separated ids.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Bulk insert for links
@Jakob, ooh that’s a nice idea !!
Meanwhile I’ve started writing some code. I’ve hit a road block: it seems that my favorite dev method, var_dump($myThing); die;
won’t work on article_saved :(
I have declared my function :
register_callback(array(__CLASS__, "bulkInsert"), "article", "save", 1);
public static function bulkInsert()
{
$message = 'textpattern.Console.addMessage(['.json_encode(ps('plh_reference_links'), TEXTPATTERN_JSON).', 0]);'.n;
send_script_response($message);
}
I’ve tried send_script_response
with no results.
What the best way to output some variable in the callback ?
Thanks for your help :)
edited to fix the callback args
Last edited by planeth (2024-08-20 15:34:52)
Offline
Re: Bulk insert for links
Hmm, it should work with the exit
in place, as should the ability to send it over send_script_response
. That’s odd. Can you perhaps try adding this to your config.php:
define('txpdmpfile', 'debug.txt');
and then refresh. That should divert any dmp()
output into /path/to/your/tmpdir/debug.txt.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Bulk insert for links
Sorry :/ I’ve been testing all the combinations possible. No debug.txt in my tempdir :/
would there be any glitch in the way I declare the callback ?
register_callback(array(__CLASS__, "bulkInsert"), "article", "save", 1);
Offline