Mediathek übernehmen

This commit is contained in:
Friederich Loheide 2026-09-26 20:14:01 +00:00
commit 655700c71a
46 changed files with 3101 additions and 89 deletions

View file

@ -4,6 +4,8 @@ namespace Inhaus\Plugins\News;
use App\Dashboard\Widgets;
use App\Editorial\Editorial;
use App\Media\ImagePaths;
use App\Media\MediaUsages;
use App\Models\User;
use App\Pagebuilder\Blocks;
use App\Pagebuilder\BlockType;
@ -15,6 +17,7 @@ use App\Support\Navigation;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schema;
use Inhaus\Plugins\News\Http\Resources\NewsPostResource;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
@ -40,6 +43,7 @@ class NewsPlugin extends Plugin
Gate::policy(NewsPost::class, NewsPostPolicy::class);
$this->registerWidgets($this->app->make(Widgets::class));
$this->registerMediaUsages($this->app->make(MediaUsages::class));
$this->app->make(Editorial::class)->add(
'news',
@ -70,6 +74,41 @@ class NewsPlugin extends Plugin
}));
}
/**
* Mediathek „Eingebunden in“: Titelbild, Bilder im Text und – sobald Beiträge
* mit dem Pagebuilder geschrieben werden – Bilder in der Spalte „content“.
*/
private function registerMediaUsages(MediaUsages $usages): void
{
$usages->add('news', 'Neuigkeiten', function (NewsPost $post) {
$base = ['label' => $post->title, 'url' => route('news.editor.edit', $post, false), 'draft' => $post->status !== NewsPost::PUBLISHED];
$entries = [
[...$base, 'context' => 'Titelbild', 'paths' => [$post->cover_path]],
[...$base, 'context' => ImagePaths::HTML_CONTEXT, 'paths' => [...ImagePaths::fromHtml($post->body_html), ...ImagePaths::fromHtml($post->body)]],
];
if (self::hasContentColumn()) {
$content = $post->getAttribute('content');
$content = is_string($content) ? json_decode($content, true) : $content;
foreach (ImagePaths::fromContent(is_array($content) ? $content : null) as $context => $paths) {
$entries[] = [...$base, 'context' => $context, 'paths' => $paths];
}
}
return $entries;
}, fn (callable $each) => NewsPost::query()->chunkById(100, fn ($posts) => $posts->each($each)));
$usages->watch('news', NewsPost::class);
}
private static function hasContentColumn(): bool
{
static $has;
return $has ??= Schema::hasColumn('news_posts', 'content');
}
private function registerWidgets(Widgets $widgets): void
{
// Slider: hervorgehobene Beiträge mit Bild, sonst die neuesten mit Bild.