How to add option to hide content element in mobile or small screen on TYPO3.

Here I am extending the table tt_content to have the checkbox "Hide in mobile" on every content element.

On ext_tables.sql of custom extension,

CREATE TABLE tt_content (
   hidein_phone tinyint(4) DEFAULT '0' NOT NULL
);

 

In ext_tables.php

$tempColumns = array(
  'hidein_phone' => array(
    'exclude' => 1,
    'label' => 'Hide in phone',
    'config' => array (
      'type' => 'check',
      'default' => '0'
    )
   ),
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
   'tt_content',
   $tempColumns,
  1
);

 

#add field in specific place
$myPallet = '--linebreak--,hidein_phone';
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToPalette(
   'tt_content',
   'frames',
   $myPallet,
   'after:section_frame'
);

In TS template

tt_content.stdWrap.innerWrap.cObject.default.20.50 = TEXT
tt_content.stdWrap.innerWrap.cObject.default.20.50 {
   value = hidden-phone
   noTrimWrap = | ||
   if.isTrue.field = hidein_phone
}

The above typoscript adds the class "hidden-phone" to content element if the checkbox is checked.

Finally in css we can add this code,

@media (max-width: 767px) {
  .hidden-phone{
      display:none;
  }
}

 

That's it.