Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-05-01 19:54:28

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 141
Website

Help with enhanced duplicate function/plugin

I am trying to build a plugin out of a tweak I already succesfully managed to do testwise in textpattern/include/txp_article.php.

What I wanted to achieve is:

When using textpatterns native duplicate link in the write pane the current article is not only duplicated but the ID of the original article should be written into the custom_1 field. I need this feature for a multi-lingual setup. So the relation between articles is given via the custom_field instantly.

This works already well with the following changes in textpattern/include/txp_article.php

if ($is_clone) {
       $clone_id = $incoming['ID'];
        $incoming['ID'] = $incoming['url_title'] = '';
        $incoming['Status'] = STATUS_DRAFT;
    }

I added:

  $clone_id = $incoming['ID'];

So that the current ID can be later still be used to fill the custom_1
Which I do by finding:

if ($is_clone) {
                $url_title = stripSpace($Title_plain.' ('.$rs['ID'].')', 1);
                safe_update(
                    'textpattern',
                    "Title = CONCAT(Title, ' (', ID, ')'),
                    custom_1 = ".$clone_id.",
                    url_title = '$url_title'",
                    "ID = ".$rs['ID']
                );
            }

On which I added the line:

 custom_1 = ".$clone_id.",

That would already work with the original duplicate link. But as I wanted to make a plugin out of it I started my plugin code with:

register_callback('dem_clone_article', 'article_ui', 'sort_display');
function dem_clone_article($event, $step, $data, $rs) {
	extract($rs);
    $clone_link = n.href('<span class="ui-icon ui-icon-copy"></span> dem_'.gTxt('duplicate'), '#', array(
        'class' => 'txp-clone',
        'id'    => 'article_partial_article_clone',
    ));
    return $clone_link.$data;
}

Which gives me an extra link on the sidebar above the “Sort and display” box.
The question is – and sorry for my lack of knowledge here – how can I get the same functionality into my plugin. Without hacking the core code. Which is of course a bad practice and I did it only for testing on how this could work at all. I am struggling to get any further here. Would I have to implement (duplicate) the whole saving process into the plugin? Any push in the right direction would be highly appreciated.

Offline

#2 2021-05-02 08:41:55

etc
Developer
Registered: 2010-11-11
Posts: 4,265
Website GitHub

Re: Help with enhanced duplicate function/plugin

Hello,

I think you can do it in a less intrusive way, by altering custom_1 value via JS on ‘duplicate’ link click. Let us know if it works or you need more detailed instructions.

Offline

#3 2021-05-02 10:29:45

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 141
Website

Re: Help with enhanced duplicate function/plugin

Wow. That seems to be so much better, yes. Thank you. I managed to do it by creating this JS in the admin themes assets/js/custom.js

function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;
    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] === sParam) {
            return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
    return false;
};
$(document).ready(function() {	
var origin_id = getUrlParameter('ID');	
$("#clone").click(function(event){
  $('#custom-1').val(origin_id);
});
});

Now I whish I could put this JS into my plugin file. But when I try to “echo” this in my plugin file, it fails. It is not working and shows some weird characters on the top of the write pane.
I just checked another plugin to see how JS is implemented and try to copy it but no chance.
So basically all is working. It´s just that I would like to give the world that as a complete plugin (And also make it easier for myself when I want to set this all up another time) =)

EDIT: When it is INSIDE the plugins file I might not need the getUrlParameter-function. Because the value of the ID is already there.

Last edited by demoncleaner (2021-05-02 10:34:06)

Offline

#4 2021-05-02 11:40:28

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,288
Website GitHub

Re: Help with enhanced duplicate function/plugin

Try:

echo script_js(<<<EOJS
// Your js here
EOJS
);

You can attach this to any of the admin side hooks (e.g. head_end) or perhaps better append it to the custom field pluggable_ui() call. That way, it does nothing if custom fields aren’t in use.


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

#5 2021-05-02 21:16:14

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 141
Website

Re: Help with enhanced duplicate function/plugin

Thanks Stef. I got it working.
I will do some more finetuning and then publish it.

For a 2-Language approach it would be pretty cool if when clicking the “clone to other language” not only the custom_1 is filled with the original article ID but also the section is automatically set to its “sister”-section of the other language.

Maybe I will manage to create a dropdown where you can even choose into which language you are going to clone the article and then the setting of the sections happens automatically.

I will have to see if this is too specific for a plugin who´s purpose should be ideally a more general use.
But anyway…thanks again for bringing me on the right path.

Offline

#6 2021-05-03 07:10:28

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,616
Website GitHub Twitter

Re: Help with enhanced duplicate function/plugin

This promises to be a great, and very useful plugin. I was thinking of page templates and the declared language of the article (<html lang="en-gb">). Do you have a solution which does not require a duplicate template for this? Just read your latest post which mentions that the plugin will require a second cf…

Last edited by colak (2021-05-03 07:13:50)


Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#7 2021-05-03 08:06:44

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 141
Website

Re: Help with enhanced duplicate function/plugin

This plugin is going to be part of a multi-language setup I arranged arround adi_menu.
A little tutorial can be found here: www.rappid.de/txp_ml

I am still in the process of fine-tuning it so it works, right now but I do not see it as a final state.

The problem I see with this plugin is, that if it should work really well, I need to make it dependend on adi_menu. Because adi_menu holds and handles the languages. Maybe I will create two different modes. “simple” mode and “adi_menu” mode.

Last edited by demoncleaner (2021-05-03 08:07:54)

Offline

#8 2021-05-03 08:18:05

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,288
Website GitHub

Re: Help with enhanced duplicate function/plugin

See the <txp:if_plugin> tag ;)


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

#9 2021-05-03 08:46:21

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 141
Website

Re: Help with enhanced duplicate function/plugin

Interesting. But this is a frontend tag, right?
I would need this inside the plugin. So that if adi_menu is installed it does more in the backend than if it is not installed.

Offline

#10 2021-05-03 09:16:38

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,288
Website GitHub

Re: Help with enhanced duplicate function/plugin

We don’t have a plugin_installed function yet but you can do one of two things in code, depending on your use case.

if (load_plugin('adi_menu')) {
 ...
}

Or:

parse(<<<EOTXP
<if::plugin name="adi_menu">
   ...
</if::plugin>
EOTXP
);

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

#11 2021-05-03 09:17:46

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 141
Website

Re: Help with enhanced duplicate function/plugin

Great stuff! Thanks a lot. I am on it.

Offline

Board footer

Powered by FluxBB