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

Automate sites.yaml config #81

Merged
merged 17 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 10 additions & 192 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This package helps you integrate Statamic within your Rapidez project by adding

- Products, categories and brands are integrated through [Runway](https://github.com/duncanmcclean/runway) as read only resources so you can link them to the content
- Products, categories, brands and pages collections as starting point so you can extend this however you'd like
- Site registration based on the active Magento stores
- Route merging so Statamic routes work as fallback
- Page builder fieldset with a product slider, content, image and form component
- Responsive images with Glide through [justbetter/statamic-glide-directive](https://github.com/justbetter/statamic-glide-directive)
Expand All @@ -15,6 +16,13 @@ This package helps you integrate Statamic within your Rapidez project by adding

You need to have `statamic/cms` installed in your Rapidez installation. Follow the [installation guide](https://statamic.dev/installing/laravel).

Make sure you have an existing User model as Statamic requires this.
If you don't have a User model you can get it from the [Laravel repo](https://github.com/laravel/laravel/blob/11.x/app/Models/User.php).

You will also need the migrations for the users table, this can be found [here](https://github.com/laravel/laravel/blob/11.x/database/migrations/0001_01_01_000000_create_users_table.php).
At last you need to make some changes to your User model to make it compatible with Statamic,
make sure to follow [this guide](https://statamic.dev/tips/storing-users-in-a-database#in-an-existing-laravel-app) after requiring `rapidez/statamic` in your project.

## Installation

```
Expand All @@ -24,7 +32,7 @@ composer require rapidez/statamic
## Install command

The install command will help you set up all the necessary settings.
It will mainly setup the [Eloquent driver](https://github.com/statamic/eloquent-driver) and publish the necessary vendor from the rapidez/statamic repo.
It will mainly setup the [Eloquent driver](https://github.com/statamic/eloquent-driver) and publish the necessary vendor from the rapidez/statamic repo.

```
php artisan rapidez-statamic:install
Expand All @@ -44,197 +52,7 @@ We recommend migrating the following options to the database when setting up the
- Terms
- Tokens

After you're done running the install command make sure to check the guide below for some manual changes.

## Configuration

Have a look within the `rapidez/statamic.php` config file, if you need to change something you can publish it with:

```
php artisan vendor:publish --provider="Rapidez\Statamic\RapidezStatamicServiceProvider" --tag=config
```

### Assets disk

Make sure there is an assets disk within `config/filesystems.php`
```
'disks' => [
'assets' => [
'driver' => 'local',
'root' => public_path('assets'),
'url' => '/assets',
'visibility' => 'public',
],
],
```

### Routing

As Rapidez uses route fallbacks to allow routes to be added with lower priority than Magento routes, this package is used to fix this, as statamic routes on itself will overwrite your Magento routes. Make sure default Statamic routing is disabled in `config/statamic/routes.php`. We'll register the Statamic routes from this packages after the Magento routes.

```php
'enabled' => false,
```

#### Homepage

If you'd like to use the homepage from Statamic instead of the CMS page from Magento; just disable the homepage in Magento.

### Publish Collections, Blueprints and Fieldsets

If you have run the install command these will already have been published.

```
php artisan vendor:publish --provider="Rapidez\Statamic\RapidezStatamicServiceProvider" --tag=rapidez-statamic-content
```

And if you'd like to change the views:

```
php artisan vendor:publish --provider="Rapidez\Statamic\RapidezStatamicServiceProvider" --tag=views
```


### Magento Store ID

It is important to add the Magento store ID for every site in the attributes section within `resources/sites.yaml` and use the store code as key. Because the url can vary per enviroment(local, testing, staging, production) we use the "sites" section of the config file and reference that in the `sites.yaml`. The current site will be determined based on the `MAGE_RUN_CODE`. By default Statamic uses the url for this; that's still the fallback. If you need to generate some urls with a multisite it's a best practice to specify the `url` per site from env variables. See the [Statamic multisite docs](https://statamic.dev/multi-site#url). Optionally you could set the `group` within the `attributes` if you'd like to group sites to filter the alternate hreflang link tags. You could also set the `disabled` within the `attributes` section to true if you want to exclude this site from being altered with Statamic data.
```yaml
'default' => [
'name' => env('APP_NAME', 'Statamic'),
'locale' => 'en_EN',
'lang' => 'en_EN',
'url' => '/',
'attributes' => [
'magento_store_id' => 1,
'group' => 'default',
'disabled' => false,
],
],
```


```yaml
default:
name: '{{ config:rapidez.statamic.sites.default.name }}'
locale: '{{ config:rapidez.statamic.sites.default.locale }}'
lang: '{{ config:rapidez.statamic.sites.default.lang }}'
url: '{{ config:rapidez.statamic.sites.default.url }}'
attributes:
magento_store_id: '{{ config:rapidez.statamic.sites.default.attributes.magento_store_id }}'
group: '{{ config:rapidez.statamic.sites.default.attributes.group }}'
disabled: '{{ config:rapidez.statamic.sites.default.attributes.disabled }}'
```

### Showing content on categories and products

By default you'll get the configured content on categories and products available withint the `$content` variable. This can be enabled/disabled with the `fetch` configurations within the `rapidez/statamic.php` config file. If you want to display the configured content from the default page builder you can include this in your view:
```
@includeWhen(isset($content), 'rapidez-statamic::page_builder', ['content' => $content?->content])
```
- Product: `resources/views/vendor/rapidez/product/overview.blade.php`
- Category: `resources/views/vendor/rapidez/category/overview.blade.php`


### Brand overview and single brand
#### Brand pages
Single brand pages display a listing page with your products linked to that brand. By default the single brand pages are disabled. You can enable routing for them in `content/collections/brands.yaml` by adding a route for them:

```yaml
route: '/brands/{slug}'
```
#### Brand overview
If you want an overview page for your brands you can add a `Brand overview` component on a normal page. This will automaticly load a view with your brands grouped by their first letter.

### Importing categories or products from Magento

#### Categories

To make it easier to change category content in bulk you can create category entries with content copied over in bulk.

To do this run one of the following:

```bash
# Most basic, import all categories in all sites
php artisan rapidez:statamic:import:categories --all

# Import all categories in the site with handle "default" only
php artisan rapidez:statamic:import:categories --all --site=default

# import select categories in multiple sites
php artisan rapidez:statamic:import:categories 5 8 9 category-url-key --site=default --site=another_site
```

By default the slug and title of the category are copied.

If you have a custom blueprint and would like to add more data from the category you can do so by hooking into the Eventy event: `rapidez-statamic:category-entry-data`

```php
Eventy::addFilter('rapidez.statamic.category.entry.data', fn($category) => [
'description' => $category->description,
]
);
```

#### Products

To make it easier to change product content in bulk you can create product entries with content copied over in bulk.

To do this run one of the following:

```bash
# Most basic, import all products in all sites
php artisan rapidez:statamic:import:products

# Import all products in the site with handle "default" only
php artisan rapidez:statamic:import:products --site=default
```

By default the slug and title of the product are copied.

If you have a custom blueprint and would like to add more data from the product you can do so by hooking into the Eventy event: `rapidez.statamic.product.entry.data`

```php
Eventy::addFilter('rapidez.statamic.product.entry.data', fn($product) => [
'description' => $product->description,
]
);
```

#### Brands

To make it easier to change brands content in bulk you can create brand entries with content copied over in bulk.

To do this run one of the following:

```bash
# Import all brands in all sites
php artisan rapidez:statamic:import:brands

# Import all brands in the site with handle "default" only
php artisan rapidez:statamic:import:brands --site=default
```

By default the slug and title of the brand are copied.

If you have a custom blueprint and would like to add more data from the brand you can do so by hooking into the Eventy event `rapidez.statamic.brand.entry.data`

```php
Eventy::addFilter('rapidez.statamic.brand.entry.data', fn($brand) => [
'description' => $brand->description,
]
);
```


### Globals

Globals will be available through the `$globals` variable.
For example; If you created a global with the handle `header` and added a field called `logo` in this global it will be available as `$globals->header->logo`.


### Forms

When you create a form you could use `rapidez-statamic::emails.form` as HTML template which uses the [Laravel mail template](https://laravel.com/docs/master/mail#customizing-the-components) with all fields in a table, make sure you enable markdown!
After you're done running the install command make sure to check our configuration guide written [here](https://docs.rapidez.io/3.x/packages/statamic.html).

### Upgrading

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"rapidez/blade-directives": "^0.6",
"justbetter/statamic-glide-directive": "^2.1",
"statamic-rad-pack/runway": "^7.6",
"statamic/cms": "^5.0",
"statamic/cms": "^5.29",
"statamic/eloquent-driver": "^4.9",
"tormjens/eventy": "^0.8"
},
Expand Down
3 changes: 0 additions & 3 deletions config/rapidez/statamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@

'sites' => [
kevinmeijer97 marked this conversation as resolved.
Show resolved Hide resolved
'default' => [
'name' => env('APP_NAME', 'Statamic'),
'locale' => 'en_EN',
'lang' => 'en_EN',
'url' => '/',
'attributes' => [
'magento_store_id' => 1,
'group' => 'default',
'disabled' => false,
],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this whole sites config?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this around, see #81 (comment)

],
Expand Down
36 changes: 36 additions & 0 deletions src/Extend/SitesLinkedToMagentoStores.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rapidez\Statamic\Extend;

use Illuminate\Support\Facades\Cache;
use Rapidez\Core\Facades\Rapidez;
use Statamic\Sites\Sites;

class SitesLinkedToMagentoStores extends Sites
Expand All @@ -20,4 +22,38 @@ public function findByMageRunCode($code)
{
return collect($this->sites)->get($code);
}

protected function getSavedSites()
{
return Cache::rememberForever('statamic_sites', function () {
kevinmeijer97 marked this conversation as resolved.
Show resolved Hide resolved
$sites = [];
$stores = Rapidez::getStores();
$configModel = config('rapidez.models.config');

foreach ($stores as $store) {
if (config('rapidez.statamic.sites.' . $store['code'] . '.attributes.disabled')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we safely say we always want this to be enabled? And otherwise maybe just have an array that includes the disabled stores?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this around, see #81 (comment)

continue;
}

Rapidez::setStore($store['store_id']);

$locale = $configModel::getCachedByPath('general/locale/code');
$lang = explode('_', $locale)[0] ?? '';
$url = $configModel::getCachedByPath('web/secure/base_url');
kevinmeijer97 marked this conversation as resolved.
Show resolved Hide resolved

$sites[$store['code']] = [
'name' => $store['name'] ?? $store['code'],
'locale' => $locale,
'lang' => $lang,
'url' => $url,
'attributes' => [
'magento_store_id' => $store['store_id'],
'group' => $store['website_code'] ?? ''
kevinmeijer97 marked this conversation as resolved.
Show resolved Hide resolved
]
];
}

return $sites ?: $this->getFallbackConfig();
kevinmeijer97 marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
9 changes: 3 additions & 6 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Rapidez\Statamic;

use Statamic\Statamic;
use Statamic\Sites\Sites;
use Statamic\Facades\Site;
use Statamic\Facades\Entry;
Expand All @@ -17,11 +16,11 @@
use Statamic\Events\GlobalSetDeleted;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View as RenderedView;
use Rapidez\Statamic\Commands\ImportBrands;
use Rapidez\Statamic\Commands\InstallCommand;
use Rapidez\Statamic\Forms\JsDrivers\Vue;
use Rapidez\Statamic\Commands\ImportBrands;
use Rapidez\Statamic\Commands\ImportProducts;
use Rapidez\Statamic\Commands\ImportCategories;
use Rapidez\Statamic\Forms\JsDrivers\Vue;
use Rapidez\Statamic\Extend\SitesLinkedToMagentoStores;
use Rapidez\Statamic\Http\Controllers\ImportsController;
use Rapidez\Statamic\Http\Controllers\StatamicRewriteController;
Expand All @@ -32,9 +31,7 @@ class RapidezStatamicServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->extend(Sites::class, function () {
return new SitesLinkedToMagentoStores(config('statamic.sites'));
});
$this->app->extend(Sites::class, fn () => new SitesLinkedToMagentoStores());
kevinmeijer97 marked this conversation as resolved.
Show resolved Hide resolved
}

public function boot()
Expand Down