Skip to content

Commit

Permalink
[FEATURE] Provide VH to render icons for resources
Browse files Browse the repository at this point in the history
Provide a ViewHelper to render the markup to show
an icon for files and folders.

Resolves: #83942
Releases: master
Change-Id: I44a99f3e06dc35b98b3e29f509432a8575c29807
Reviewed-on: https://review.typo3.org/55759
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: TYPO3com <no-reply@typo3.com>
  • Loading branch information
georgringer authored and NeoBlack committed Feb 28, 2018
1 parent c5d57fe commit 537d74c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Core\ViewHelpers;

/*
* This file is part of the TYPO3 CMS project.
*
* 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.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Resource\ResourceInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* Displays icon for a FAL resource (file or folder means a TYPO3\CMS\Core\Resource\ResourceInterface)
*/
class IconForResourceViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* View helper returns HTML, thus we need to disable output escaping
*
* @var bool
*/
protected $escapeOutput = false;

/**
* Initializes the arguments
*/
public function initializeArguments()
{
$this->registerArgument('resource', ResourceInterface::class, 'Resource', true);
$this->registerArgument('size', 'string', 'The icon size', false, Icon::SIZE_SMALL);
$this->registerArgument('overlay', 'string', 'Overlay identifier', false, null);
$this->registerArgument('options', 'array', 'An associative array with additional options', false, []);
$this->registerArgument('alternativeMarkupIdentifier', 'string', 'Alternative markup identifier', false, null);
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
$resource = $arguments['resource'];
$size = $arguments['size'];
$overlay = $arguments['overlay'];
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
return $iconFactory->getIconForResource($resource, $size, $overlay, $arguments['options'])->render($arguments['alternativeMarkupIdentifier']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. include:: ../../Includes.txt

====================================================
Deprecation: #83942 - Deprecated FileFacade::getIcon
====================================================

See :issue:`83942`

Description
===========

The method :php:`\TYPO3\CMS\Filelist\FileFacade::getIcon` has been marked as deprecated.


Impact
======

Calling the method :php:`\TYPO3\CMS\Filelist\FileFacade::getIcon` will trigger a deprecation warning.


Affected Installations
======================

Instances with extensions using the method :php:`\TYPO3\CMS\Filelist\FileFacade::getIcon`


Migration
=========

Either use the ViewHelper :html:`<core:iconForResource resource="{file}" />` or
:php:`GeneralUtility::makeInstance(IconFactory::class)->getIconForResource($resource)` to render a resource-based icon.

.. index:: FAL, FullyScanned
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. include:: ../../Includes.txt

=================================================================
Feature: #83942 - Provide ViewHelper to render icon for resources
=================================================================

See :issue:`83942`

Description
===========

A new ViewHelper to render the icon markup based on a FAL resource has been introduced.

Example:

.. code-block:: html

<core:iconForResource resource="{file}" />

.. index:: Backend, Fluid, ext:core
34 changes: 19 additions & 15 deletions typo3/sysext/filelist/Classes/FileFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -60,25 +62,27 @@ public function __construct(FileInterface $resource)

/**
* @return string
* @deprecated
*/
public function getIcon()
public function getIcon(): string
{
trigger_error('This method has been deprecated in v9 will be removed in TYPO3 v10, use ViewHelper <core:iconForResource /> instead', E_USER_DEPRECATED);
$title = htmlspecialchars($this->resource->getName() . ' [' . (int)$this->resource->getProperty('uid') . ']');
return '<span title="' . $title . '">' . $this->iconFactory->getIconForResource($this->resource, Icon::SIZE_SMALL) . '</span>';
}

/**
* @return \TYPO3\CMS\Core\Resource\FileInterface
*/
public function getResource()
public function getResource(): FileInterface
{
return $this->resource;
}

/**
* @return bool
*/
public function getIsEditable()
public function getIsEditable(): bool
{
return $this->getIsWritable()
&& GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->resource->getExtension());
Expand All @@ -87,15 +91,15 @@ public function getIsEditable()
/**
* @return bool
*/
public function getIsMetadataEditable()
public function getIsMetadataEditable(): bool
{
return $this->resource->isIndexed() && $this->getIsWritable() && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata');
}

/**
* @return int
*/
public function getMetadataUid()
public function getMetadataUid(): int
{
$uid = 0;
$method = '_getMetadata';
Expand All @@ -113,15 +117,15 @@ public function getMetadataUid()
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->resource->getName();
}

/**
* @return string
*/
public function getPath()
public function getPath(): string
{
$method = 'getReadablePath';
if (is_callable([$this->resource->getParentFolder(), $method])) {
Expand All @@ -134,39 +138,39 @@ public function getPath()
/**
* @return string
*/
public function getPublicUrl()
public function getPublicUrl(): string
{
return $this->resource->getPublicUrl(true);
}

/**
* @return string
*/
public function getExtension()
public function getExtension(): string
{
return strtoupper($this->resource->getExtension());
}

/**
* @return string
*/
public function getIdentifier()
public function getIdentifier(): string
{
return $this->resource->getStorage()->getUid() . ':' . $this->resource->getIdentifier();
}

/**
* @return string
*/
public function getLastModified()
public function getLastModified(): string
{
return BackendUtility::date($this->resource->getModificationTime());
}

/**
* @return string
*/
public function getSize()
public function getSize(): string
{
return GeneralUtility::formatSize($this->resource->getSize(), htmlspecialchars($this->getLanguageService()->getLL('byteSizeUnits')));
}
Expand Down Expand Up @@ -249,7 +253,7 @@ public function getIsImage()
*
* @return int
*/
public function getReferenceCount()
public function getReferenceCount(): int
{
$uid = (int)$this->resource->getProperty('uid');

Expand Down Expand Up @@ -303,15 +307,15 @@ public function __call($method, $arguments)
/**
* @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
*/
protected function getBackendUser()
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}

/**
* @return \TYPO3\CMS\Core\Localization\LanguageService
*/
protected function getLanguageService()
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h1><f:translate id="search">Search</f:translate>: "{searchWord}"</h1>
<tr data-uid="0" data-l10nparent="0">
<td class="col-icon nowrap">
<a class="t3js-contextmenutrigger" data-uid="{file.identifier}" data-table="sys_file" href="#">
{file.icon->f:format.raw()}
<core:iconForResource resource="{file.resource}" />
</a>
</td>
<td class="col-path nowrap">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,4 +1605,11 @@
'Deprecation-83904-ArrayHandlingInAbstractTreeView.rst',
],
],
'TYPO3\CMS\Filelist\FileFacade->getIcon' => [
'numberOfMandatoryArguments' => 0,
'maximumNumberOfArguments' => 0,
'restFiles' => [
'Deprecation-83942-DeprecatedFileFacadegetIcon.rst',
],
],
];

0 comments on commit 537d74c

Please sign in to comment.