You are here

class HideDateTime in Field Formatter Condition 8

The plugin for check empty fields.

Plugin annotation


@FieldFormatterCondition(
  id = "hide_date_time",
  label = @Translation("Hide date/time"),
  dsFields = TRUE,
  types = {
    "datetime",
    "date",
    "datestamp"
  }
)

Hierarchy

Expanded class hierarchy of HideDateTime

File

src/Plugin/Field/FieldFormatter/Condition/HideDateTime.php, line 20

Namespace

Drupal\fico\Plugin\Field\FieldFormatter\Condition
View source
class HideDateTime extends FieldFormatterConditionBase {

  /**
   * {@inheritdoc}
   */
  public function alterForm(&$form, $settings) {
    $default_orientation = isset($settings['settings']['orientation']) ? $settings['settings']['orientation'] : NULL;
    $default_cutom_date = isset($settings['settings']['cutom_date']) ? $settings['settings']['cutom_date'] : NULL;
    $form['orientation'] = [
      '#title' => t('Hide if'),
      '#type' => 'radios',
      '#options' => array(
        'smaller' => t("smaller than today's date"),
        'greater' => t("greater than today's date"),
        'custom_small' => t("smaller then custom date"),
        'greater_small' => t("greater then custom date"),
      ),
      '#default_value' => $default_orientation,
    ];
    $form['cutom_date'] = [
      '#title' => t('Cutom date'),
      '#type' => 'date',
      '#default_value' => $default_cutom_date,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function access(&$build, $field, $settings) {
    $custom_date = strtotime($settings['settings']['cutom_date']);
    if (!empty($build[$field]['#items'])) {
      foreach ($build[$field]['#items'] as $item) {
        $info = $item
          ->getValue($field);
        switch ($settings['settings']['orientation']) {
          case 'smaller':
            if (strtotime($info['value']) < REQUEST_TIME) {
              $build[$field]['#access'] = FALSE;
            }
            break;
          case 'greater':
            if (strtotime($info['value']) > REQUEST_TIME) {
              $build[$field]['#access'] = FALSE;
            }
            break;
          case 'custom_small':
            if (strtotime($info['value']) < $custom_date) {
              $build[$field]['#access'] = FALSE;
            }
            break;
          case 'greater_small':
            if (strtotime($info['value']) > $custom_date) {
              $build[$field]['#access'] = FALSE;
            }
            break;
          default:
            $build[$field]['#access'] = FALSE;
        }
      }
    }
    if (empty($build[$field]['#items'])) {
      $build[$field]['#access'] = FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function summary($settings) {
    $orientations = array(
      'smaller' => t("smaller than today's date"),
      'greater' => t("greater than today's date"),
      'custom_small' => t("smaller then custom date"),
      'greater_small' => t("greater then custom date"),
    );
    if ($settings['settings']['orientation'] != 'smaller' && $settings['settings']['orientation'] != 'greater') {
      $display_date = ' - ' . $settings['settings']['cutom_date'];
    }
    else {
      $display_date = '';
    }
    return t('Condition: %condition (%orientation%date)', [
      "%condition" => t('Hide date/time'),
      '%orientation' => $orientations[$settings['settings']['orientation']],
      '%date' => $display_date,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldFormatterConditionBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance
FieldFormatterConditionBase::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryInterface::getDefinition
FieldFormatterConditionBase::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryInterface::getDefinitions
FieldFormatterConditionBase::getEntity protected function Check for entity in build.
FieldFormatterConditionBase::getEntityFields protected function Load fields from a entity.
FieldFormatterConditionBase::getEntityType protected function Check for entity_type in build.
FieldFormatterConditionBase::getInstance public function Gets a preconfigured instance of a plugin. Overrides MapperInterface::getInstance
FieldFormatterConditionBase::hasDefinition public function Indicates if a specific plugin definition exists. Overrides DiscoveryInterface::hasDefinition
HideDateTime::access public function Access control function. Overrides FieldFormatterConditionBase::access
HideDateTime::alterForm public function Alter the condition form. Overrides FieldFormatterConditionBase::alterForm
HideDateTime::summary public function Return the summary string. Overrides FieldFormatterConditionBase::summary
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92