-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '11.5-alttext' into '11.5'
Alt text functionality - TYPO3 10/11 See merge request typo3-commons/mkcontentai!19
- Loading branch information
Showing
36 changed files
with
952 additions
and
128 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Classes/Backend/EventListener/CustomFileControlsEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* Copyright notice | ||
* | ||
* (c) DMK E-BUSINESS GmbH <[email protected]> | ||
* All rights reserved | ||
* | ||
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
*/ | ||
|
||
namespace DMK\MkContentAi\Backend\EventListener; | ||
|
||
use TYPO3\CMS\Backend\Form\Event\CustomFileControlsEvent; | ||
use TYPO3\CMS\Backend\Form\NodeFactory; | ||
use TYPO3\CMS\Core\Imaging\Icon; | ||
use TYPO3\CMS\Core\Imaging\IconFactory; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
final class CustomFileControlsEventListener | ||
{ | ||
/** | ||
* @var NodeFactory | ||
*/ | ||
public $nodeFactory; | ||
|
||
/** | ||
* @var IconFactory | ||
*/ | ||
public $iconFactory; | ||
|
||
public function __construct() | ||
{ | ||
$this->nodeFactory = GeneralUtility::makeInstance(NodeFactory::class); | ||
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class); | ||
} | ||
|
||
public function handleEvent(CustomFileControlsEvent $event): void | ||
{ | ||
$item = ' <div class="form-control-wrap"><button type="button" class="btn btn-default t3js-prompt" id="prompt">'; | ||
$item .= $this->iconFactory->getIcon('actions-image', Icon::SIZE_SMALL)->render().' '; | ||
$item .= htmlspecialchars('AI generation of image by text prompt'); | ||
$item .= '</button></div>'; | ||
|
||
$event->addControl($item); | ||
|
||
$pageRenderer = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class); | ||
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Mkcontentai/BackendPrompt'); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
Classes/Backend/Form/Element/InputTextWithAiAltTextSupportElement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* Copyright notice | ||
* | ||
* (c) DMK E-BUSINESS GmbH <[email protected]> | ||
* All rights reserved | ||
* | ||
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
*/ | ||
|
||
namespace DMK\MkContentAi\Backend\Form\Element; | ||
|
||
use TYPO3\CMS\Backend\Form\Element\InputTextElement; | ||
use TYPO3\CMS\Core\Imaging\Icon; | ||
use TYPO3\CMS\Core\Imaging\IconFactory; | ||
use TYPO3\CMS\Core\Information\Typo3Version; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class InputTextWithAiAltTextSupportElement extends InputTextElement | ||
{ | ||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function render(): array | ||
{ | ||
$resultArray = parent::render(); | ||
|
||
if ('sys_file_reference' !== $this->data['tableName'] || 'alternative' !== $this->data['fieldName']) { | ||
return $resultArray; | ||
} | ||
|
||
$html = explode(LF, $resultArray['html']); | ||
$fileUid = $this->data['databaseRow']['uid_local'][0]['uid']; | ||
$typoThreeVersion = GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion(); | ||
$pageLanguageUid = $this->data['databaseRow']['sys_language_uid'][0]; | ||
|
||
if (11 === $typoThreeVersion) { | ||
$pageLanguageUid = $this->data['databaseRow']['sys_language_uid']; | ||
} | ||
|
||
$iconFactory = GeneralUtility::makeInstance(IconFactory::class); | ||
$item[] = ' <div data-uid-local="'.$fileUid.'" data-sys-language-uid="'.$pageLanguageUid.'"class="formengine-field-item t3js-formengine-field-item form-description"> | ||
<button type="button" class="btn btn-default t3js-prompt alt-refresh"> | ||
<span class="spinner-border spinner-border-sm" style="display: none"></span>'; | ||
$item[] = $iconFactory->getIcon('actions-image', Icon::SIZE_SMALL)->render().' '; | ||
$item[] = htmlspecialchars('Generate alt text by AI'); | ||
$item[] = '</button></div>'; | ||
|
||
array_splice($html, 3, 0, $item); | ||
$resultArray['html'] = implode(LF, $html); | ||
$resultArray['requireJsModules'][] = 'TYPO3/CMS/Mkcontentai/AltText'; | ||
|
||
return $resultArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* Copyright notice | ||
* | ||
* (c) DMK E-BUSINESS GmbH <[email protected]> | ||
* All rights reserved | ||
* | ||
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
*/ | ||
|
||
namespace DMK\MkContentAi\Controller; | ||
|
||
use DMK\MkContentAi\Service\AiAltTextService; | ||
use DMK\MkContentAi\Service\SiteLanguageService; | ||
use TYPO3\CMS\Backend\Routing\UriBuilder; | ||
use TYPO3\CMS\Core\Messaging\AbstractMessage; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Domain\Model\File; | ||
|
||
/** | ||
* This file is part of the "DMK Content AI" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* (c) 2023 | ||
*/ | ||
|
||
/** | ||
* ImageController. | ||
*/ | ||
class AiTextController extends BaseController | ||
{ | ||
public AiAltTextService $aiAltTextService; | ||
public SiteLanguageService $siteLanguageService; | ||
|
||
public function __construct(AiAltTextService $aiAltTextService, SiteLanguageService $siteLanguageService) | ||
{ | ||
$this->aiAltTextService = $aiAltTextService; | ||
$this->siteLanguageService = $siteLanguageService; | ||
} | ||
|
||
public function altTextAction(File $file): void | ||
{ | ||
$this->view->assignMultiple( | ||
[ | ||
'file' => $file, | ||
'altText' => $this->getAltTextForFile($file), | ||
'languageName' => $this->siteLanguageService->getFullLanguageName(), | ||
] | ||
); | ||
} | ||
|
||
public function altTextSaveAction(File $file): void | ||
{ | ||
$altText = $this->getAltTextForFile($file); | ||
|
||
$metadata = $file->getOriginalResource()->getMetaData(); | ||
$metadata->offsetSet('alternative', $altText); | ||
$metadata->save(); | ||
|
||
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); | ||
$metaDataUid = $file->getOriginalResource()->getMetaData()->get()['uid']; | ||
$editUrl = $uriBuilder->buildUriFromRoute('record_edit', [ | ||
'edit[sys_file_metadata]['.$metaDataUid.']' => 'edit', | ||
]); | ||
$this->redirectToUri($editUrl); | ||
} | ||
|
||
private function getAltTextForFile(File $file): string | ||
{ | ||
$altTextFromFile = ''; | ||
|
||
try { | ||
$altTextFromFile = $this->aiAltTextService->getAltText($file); | ||
} catch (\Exception $e) { | ||
$this->addFlashMessage($e->getMessage(), '', AbstractMessage::ERROR); | ||
} | ||
|
||
return $altTextFromFile; | ||
} | ||
} |
Oops, something went wrong.