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

\Statamic\Taxonomies\Term::dataForLocale() throws Exception when no defaultLocale data is provided #9376

Closed
markusreis-yd opened this issue Jan 23, 2024 · 1 comment

Comments

@markusreis-yd
Copy link

Bug description

When I create a new Term and try to save data for a term in a specific locale that does not exist in the defaultLocale (which is just selected by being the first linked site $this->taxonomy()->sites()->first()), vendor/statamic/cms/src/Taxonomies/Term.php:112 will throw Call to a member function all() on null, since there is no data for the defaultLocale.

In my opinion, terms should also have the option the be site-specific, as well as being able to be in draft or published state. If I create article categories for example, I might not want to show the same archive pages in every site I manage through the CMS. Currently, when adding an entry through the CMS directly, it will automatically be available in all sites.

How to reproduce

Create a taxonomy and within that taxonomy create a term. For that term, only add data in a locale that is not the first linked site to that term.

/** @var \Statamic\Taxonomies\Term $term */
$term = Term::make()->taxonomy('example-taxonomy')->slug('example-slug');

$term->dataForLocale(
    'not-default-site',
    ['title' => 'Example Title']
);

$term->save();

Logs

Error 

  Call to a member function all() on null

  at vendor/statamic/cms/src/Taxonomies/Term.php:112
    108▕     {
    109▕         $localizations = clone $this->data;
    110▕ 
    111▕         $array = Arr::removeNullValues(
  ➜ 112▕             $localizations->pull($this->defaultLocale())->all()
    113▕         );
    114▕ 
    115▕         // todo: add published bool (for each locale?)
    116▕ 


### Environment

```yaml
Environment
Application Name: Laravel
Laravel Version: 9.52.16
PHP Version: 8.1.27
Composer Version: 2.6.6
Environment: local
Debug Mode: ENABLED
URL: localhost
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Scout: meilisearch
Session: file

Statamic
Addons: 2
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.44.0 PRO

Statamic Addons
statamic-rad-pack/meilisearch: 3.1.0
stillat/relationships: 2.1.3

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

None

Additional details

I am not certain if this should be a feature-request or a bug-report, but since it made my code crash and I had too dig a bit to understand the issue I feel like it is a bug for now. The modification to have publishing logic for taxonomies just like we have it for collections might be moved to a feature request. Let me know.

Thanks!
Markus

@markusreis-yd markusreis-yd changed the title \Statamic\Taxonomies\Term::dataForLocale() throws Exception when no defaultLocale Date is provided \Statamic\Taxonomies\Term::dataForLocale() throws Exception when no defaultLocale data is provided Jan 23, 2024
@jasonvarga
Copy link
Member

In my opinion, terms should also have the option the be site-specific

I agree, and we will hopefully address this in statamic/ideas#839 but for now, this is just how it works. Terms must exist in the default site and they can be localized into the others.

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

No branches or pull requests

3 participants