Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title from related entry field should bubble up #4579

Closed
JeanLucEsser opened this issue Jul 16, 2019 · 10 comments
Closed

Title from related entry field should bubble up #4579

JeanLucEsser opened this issue Jul 16, 2019 · 10 comments

Comments

@JeanLucEsser
Copy link

Description

It is really nice that you can define your own Title in an entry as {myOtherField}.

But it falls apart when we try using a related entry, as in {myRelatedEntry.one().title}. When changing that related entry title, it doesn't update the main entry title as would be expected.

Steps to reproduce

  1. Define an entry with a related entry field myRelatedEntry
  2. Update the entry title to be {myRelatedEntry.one().title}
  3. Create an entry and save it, then change the related entry title which won't be reflected

Additional info

  • Craft version: 3.2.2 (latest)
@JeanLucEsser JeanLucEsser changed the title Title from related entry should bubble up Title from related entry field should bubble up Jul 16, 2019
@brandonkelly
Copy link
Member

I’m not able to reproduce this; the title is continually updated as I change the value of the Entries field.

Can you send your composer.json and composer.lock files, and a database backup, to [email protected] and reference this GitHub issue? We can help look into it from there.

@JeanLucEsser
Copy link
Author

@brandonkelly Have you tried changing the value of the related entry field's title? My issue is specific to fields of a related entry. Not fields of the actual entry. Will email support with screen capture as well. Thanx.

@brandonkelly
Copy link
Member

Sorry I had missed the word “title” in step 3; I only tested changing the actual related entry selection.

I’m able to reproduce when editing an entry draft, if no additional changes are then made to the draft.

Note that in Craft 3.2.3, entries are now immediately savable, whether or not Craft thinks you’ve made any changes (#4535). So even if Craft doesn’t pick up the title change right away, you can still click Save Entry, which will update the entry’s title.

@brandonkelly brandonkelly reopened this Jul 16, 2019
@JeanLucEsser
Copy link
Author

Yes if I force save the main entry A the title change from related entry B is picked up. But I would expect not having to go to entry A and force save it if related entry B is updated on its own or by another member of the team. Would you agree?

@brandonkelly
Copy link
Member

Just looked into fixing this but unfortunately because the value of the field doesn’t change, there’s no clear indication that the draft needs to be updated after changing a related entry’s title. I don’t really want to force-resave the draft, since the chances are low that it will actually matter, and especially now that the lack of change detection won’t prevent you from saving the entry (which will definitely update the title), I think this is safe to close. The only awkwardness is that the draft’s title won’t update until you make another change. But if this is all you’re changing, you wouldn’t even be creating a draft to begin with in 3.2.3+.

@JeanLucEsser
Copy link
Author

True, my only remaining concern is as I said earlier, if the related entry B gets updated on its own by another member of the team. Would I need to know that and act on entry A to reflect the change? Couldn't saving entry B actually 'reverse' update the title for entry A? Or is this not possible? Not sure I'm making myself clear here.

@brandonkelly
Copy link
Member

Sorry that’s not possible; Craft isn’t really aware of Entry A’s dependency on Entry B for its Title Format.

@JeanLucEsser
Copy link
Author

Ok I get that. Guess I could write a cron to periodically force save all entries A so that I'm sure it always reflect the correct title from related entry B. Thanx for your valued input Brandon, as always. Best.

@brandonkelly
Copy link
Member

You could also set it up with a module:

use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\helpers\ElementHelper;
use yii\base\Event;

Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function(ModelEvent $e) {
    /** @var Entry $entry */
    $entry = $e->sender;

    // Was this entry in Section B, and not a draft/revision/resave/site propagation?
    if (
        !ElementHelper::isDraftOrRevision($entry) &&
        !$entry->propagating &&
        !$entry->resaving &&
        $entry->section->handle === 'section-b'
    ) {
        // Resave all related entries from Section A
        $relatedEntryQuery = Entry::find()
            ->section('section-a')
            ->relatedTo(['targetElement' => $entry, 'field' => 'myEntriesFieldHandle']);
        Craft::$app->elements->resaveElements($relatedEntryQuery);
    }
});

@JeanLucEsser
Copy link
Author

That's awesome! Still so much stuff to learn ;)
You guys are above amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants