TCA

Integrate the code from below to your TCA file my_table.php. Replace "my_table" with the name of your table.

The php sample from below contains one relation to sys_categories. Adapt the relation to your needs.

Ctrl

Code in the ctrl section:

$TCA = [
  'ctrl' => [
    ...
    // Configuration EXT:multisite begin
    'extensions' => [
      'multisite' => [
        'enabled' => true,
        // Optional. Needed only, if you use another field name than rootpid
        // 'rootpid' => 'my_rootpid',
      ]
    ],
    // Configuration EXT:multisite end
    ...

Column

Code in the column section

Quick solution

Quick solution:

  • You need one line TCA only.

  • But your extension depends on EXT:multisite and it will not run without multisite any longer.

$TCA = [
  ...
  'columns' => [
    ...
    'sys_categories' => [
      ...
      'config' => [
        ...
        'foreign_table_where' => ''
          . 'AND (sys_category.sys_language_uid = 0 OR sys_category.l10n_parent = 0) '
          // Configuration EXT:multisite begin
          . 'AND ' . $tableName . '.' . \Verdigado\Multisite\Utility\TcaTablesUtility::GetRootpidLabel('sys_category') . ' = ###SITEROOT### '
          // Configuration EXT:multisite end
        ...

Professional solution

Own PHP class plus TCA configuration.

Your extension doesn't depend on EXT:multisite. Your extension will run proper in both cases:

  • EXT:multisite isn't installed: TCA configuration from below hasn't any effect.

  • EXT:multisite is installed: TCA configuration from below has an effect.

PHP class in the directory Class/TCA of your extension:

<?php

namespace MyProviderName\MyExtension\TCA;

class Rootpid
{

  /**
   * Returns an andWhere statement for the TCA configuration foreign_table_where.
   * If EXT:multisite isn't installed, the method returns nothing.
   *
   * @param string  $tableName Foreign table
   * @return mixed
   */
  static public function AndWhere($tableName)
  {
    if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('multisite'))
    {
      return;
    }

    $rootpid = \Verdigado\Multisite\Utility\TcaTablesUtility::GetRootpidLabel($tableName);
    if (!empty($rootpid))
    {
      $andWhere = 'AND ' . $tableName . '.' . $rootpid . ' = ###SITEROOT### ';
      return $andWhere;
    }

    $prompt = 'FATAL ERROR: rootpid is empty. Table ' . $tableName . '. '
            . 'Error occurs @ ' . __METHOD__ . '#' . __LINE__
    ;
    die($prompt);
  }

}

TCA configuration:

$TCA = [
  ...
  'columns' => [
    ...
    'sys_categories' => [
      ...
      'config' => [
        ...
        'foreign_table_where' => ''
          . 'AND (sys_category.sys_language_uid = 0 OR sys_category.l10n_parent = 0) '
          // Configuration EXT:multisite begin
          . MyProviderName\MyExtension\TCA\Rootpid::AndWhere('sys_category')
          // Configuration EXT:multisite end
        ...