Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2024-12-09 17:43:14

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,752
Website

Re: Plugin to change author on write tab

etc wrote #338464:

There is a small quirk: when editing a new article, the first (not the current) author is selected from the list. I guess you should check whether $rs['AuthorID'] is empty and then select the current user by default.

Thanks! Shows I’ve not used it much yet. Easily resolved, but first a question: Is there a reason why the “Elvis-Operator” should not be used, versus the more verbose variant? I don’t see it anywhere in our codebase. Is it just readability? e.g.

$author = $rs['AuthorID'] ?: $txp_user;

vs.

$author = !empty($rs['AuthorID']) ? $rs['AuthorID'] : $txp_user;

TXP Builders – finely-crafted code, design and txp

Online

#14 2024-12-09 20:25:23

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,475
Website GitHub

Re: Plugin to change author on write tab

There is a reason. $rs['AuthorID'] tests the state of the array entry but if, for whatever reason, that array index doesn’t exist or has been unset, it will throw an error indicating it doesn’t. Wrapping it in empty() returns true not only if it exists and is blank, but also returns true if it doesn’t exist at all, and silences the warning. In other words, empty() considers absence of the array index too. Handy.

That has the knock-on effect that Elvis has to stay outside the building :)

Last edited by Bloke (2024-12-09 20:32:46)


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

#15 2024-12-09 21:30:43

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,752
Website

Re: Plugin to change author on write tab

Bloke wrote #338466:

That has the knock-on effect that Elvis has to stay outside the building :)

Hahaha!

Thanks for the clarification. I think I read that somewhere: Elvis throws a notice error.

EDIT: The plugin is updated. Thanks for your help and for the forum co-piloting.


TXP Builders – finely-crafted code, design and txp

Online

#16 2024-12-10 05:49:16

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,243
Website

Re: Plugin to change author on write tab

jakob wrote #338463:

If you replace sort_display with categories, it gets inserted at the end of the categories region (within it rather than after it). If you want to have it within an existing region (after all, having a region with twister dropdown all to itself feels a bit extreme), you could strip the wrapRegion function out of the article_author_select() function, leaving just the inputLabel bit. But where does it fit best? Perhaps in the “Meta” region somewhere?

Yes, I noticed, a little surpised, after posting that the “author” widget was now inserted inside the “Categories” group.

From my perspective –and given the limitations of plugable_ui– inserting it in the “Meta” group seems the better option. It might be less visible than a dedicated group, but up there at the top it feels it wants to much attention.

Based on your pointers (thanks), I’ll give it a try later.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#17 2024-12-12 06:39:54

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,243
Website

Re: Plugin to change author on write tab

I finally got around trying to change the position

step 1: simplify the widget, only output the label and select. This was easy (except I first left a redundant )…).
step 2, change the position, here I put meta for the “Meta” block, but nothing happened, no field was inserted in the targeted block. I then tried again with categories, that worked but inserted the field twice (I have 2 additional authors, related?)

I noticed in include/txp_article.php that the entry for categories section has this

$html_categories = pluggable_ui('article_ui', 'categories',
$partials['categories']['html'], $rs);

no such line exist for the meta section

Last edited by phiw13 (2024-12-12 06:40:22)


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#18 2024-12-12 07:41:30

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,752
Website

Re: Plugin to change author on write tab

phiw13 wrote #338501:

I then tried again with categories, that worked but inserted the field twice (I have 2 additional authors, related?)

Interesting. It does. I don’t think it’s author-related. My guess is the callback fires for each category dropdown, for example to allow you to modify each dropdown (e.g. to remove some choices from the dropdown). The pluggable_ui function can be used to modify the existing output or, as here, append something to it.

I’m not sure of a solution here. I suppose one could check in the plugin if the callback function has already been called (e.g. set a flag) and then prevent it from functioning a second time (if the flag has already been set).

EDIT: that works. Try this in conjunction with the categories callback:

protected function article_author_select($default, $rs)
{
    global $txp_user, $jcr_author_select_has_fired;

    // Check if function has already fired and if so abort
    if ($jcr_author_select_has_fired) {
        return;
    }

    $out="";
    $author = !empty($rs['AuthorID']) ? $rs['AuthorID'] : $txp_user;
    $all_authors = array();

    foreach (safe_rows("name, RealName", 'txp_users', "1=1 ORDER BY RealName ASC") as $user) {
        extract($user);
        $all_authors[$name] = $RealName;
    }

    // Only output author select if the site has multiple authors
    if (count($all_authors) > 1) {
        $out = inputLabel(
            'authorID',
            selectInput('AuthorID', $all_authors, $author, false),
            'author',
            array('', 'instructions_authorID'),
            array('class' => 'txp-form-field authorID')
        );
    }

    // Remember that function has fired
    $jcr_author_select_has_fired = true;

    return $default.$out;
}

The changes are (alongside the dropdown-only output): add this at the top of the function:

global $txp_user, $jcr_author_select_has_fired;

if ($jcr_author_select_has_fired) {
    return;
}

and this at the bottom before the return statement:

$jcr_author_select_has_fired = true;

Maybe Stef or Oleg have a more elegant way of determining if a callback function has already been called.

I noticed in include/txp_article.php that the entry for categories section has this … but no such line exist for the meta section

You’re right: try using url_title, description or keywords depending on where you want it to appear. The callbacks for pluggable_ui are listed in the docs.


TXP Builders – finely-crafted code, design and txp

Online

#19 2024-12-12 08:10:20

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,243
Website

Re: Plugin to change author on write tab

jakob wrote #338502:

You’re right: try using url_title, description or keywords depending on where you want it to appear. The callbacks for pluggable_ui are listed in the docs.

Oh, that does it: screengrab. Great suggestion. One widget and saving works fine (at least these field: body, meta, categories so far)

I might try your other suggestions later (Placing this widget in the “Meta” block feels quite OK).

I’ll put it on my partners site, where I sometimes am an accidental author, although nobody ever sees it…)


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

Board footer

Powered by FluxBB