"New entry" menu ignores `Entry::EVENT_DEFINE_ENTRY_TYPES`, so a filtered type leads to a 400

Aperta
#20,322 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
backend

Direzione di ricerca

Start in CpAsset::_entryTypes() and compare how it builds the menu type list with EntriesController::actionCreate(), which checks getAvailableEntryTypes(). Reproduce with the supplied event listener filtering out type b, then trace how Craft.EntrySelectInput uses the same list. Done when filtered types are absent from both create controls and choosing an offered type no longer leads to this 400.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

repo:cms
Description

Entry::EVENT_DEFINE_ENTRY_TYPES (added in 3.6 for #7136) lets a site narrow the entry types available for an entry. EntriesController::actionCreate() respects it: it looks up the ?type= handle in $entry->getAvailableEntryTypes() and throws BadRequestHttpException("Invalid entry type handle: …") when the handle isn't there.

The "New entry" menu on the entries index doesn't respect it. The menu's types come from Craft.publishableSections, which CpAsset::_entryTypes() builds from $section->getEntryTypes(), so the event never runs. Any type a site filters out is still offered in the menu, and choosing it gives a 400 error page. Craft.EntrySelectInput reads the same list for an Entries field's create button, so it probably has the same problem.

This affects any restriction the event is used for, including the permission and per-site restrictions #7136 was raised for.

A possible fix: in CpAsset::_entryTypes(), make a new Entry with sectionId (and siteId) set and use getAvailableEntryTypes(), the same as actionCreate() already does, so the menu and the create action agree. Listeners would then see an entry with no ID and no parent, which they can already be given through actionCreate().

Steps to reproduce
  1. Make a section with two entry types, a and b.
  2. Register a listener that removes b:
    Event::on(Entry::class, Entry::EVENT_DEFINE_ENTRY_TYPES, function(DefineEntryTypesEvent $event) {
        $event->entryTypes = array_values(array_filter(
            $event->entryTypes,
            fn(EntryType $type) => $type->handle !== 'b',
        ));
    });
    
  3. Go to Entries, select the section, open the "New entry" menu and choose b.
Expected behavior

The "New entry" menu only offers the types the event leaves, the same as the entry type switcher and actionCreate().

Actual behavior

The menu offers b, and choosing it gives a 400: "Invalid entry type handle: b".


  • Craft CMS version: 5.11.5.1
  • PHP version: 8.4
  • Operating system and version: DDEV (Debian) on macOS
  • Database type and version: MySQL 8.4
  • Image driver and version: n/a
  • Installed plugins and versions: None needed. The listener is in a module.
Lingua principale
PHP
Stelle
3.6k
Fork
712
Merge medio
13h 20m
PR unite (30g)
357

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di craftcms/cms

Tutte le issue di craftcms/cms

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.