You are here

function rate_entity_extra_field_info in Rate 8.2

Same name and namespace in other branches
  1. 8 rate.module \rate_entity_extra_field_info()

Implements hook_entity_extra_field_info().

File

./rate.module, line 25
Hook implementation code for the Rate module.

Code

function rate_entity_extra_field_info() {
  $extra = [];

  // Rate widgets pseudo fields definitions.
  $widgets = \Drupal::service('entity_type.manager')
    ->getStorage('rate_widget')
    ->loadMultiple();
  $field_manager = \Drupal::service('entity_field.manager');

  // No need to continue without widgets.
  if (empty($widgets)) {
    return $extra;
  }
  $comment_module_enabled = \Drupal::service('module_handler')
    ->moduleExists('comment');
  if ($comment_module_enabled) {

    /** @var \Drupal\comment\CommentManagerInterface $comment_manager */
    $comment_manager = \Drupal::service('comment.manager');
  }
  foreach ($widgets as $widget => $widget_variables) {
    $entities = $widget_variables
      ->get('entity_types');
    $comments = $widget_variables
      ->get('comment_types');
    if ($entities && count($entities) > 0) {
      foreach ($entities as $id => $entity) {
        $parameter = explode('.', $entity);
        $widget_name = 'rate_' . $widget;
        $widget_label = 'Rate ' . $widget_variables
          ->label();
        $extra[$parameter[0]][$parameter[1]]['display'][$widget_name] = [
          // Create a label based on the rate widget name (duplicates are OK).
          'label' => $widget_label,
          'description' => t('Displays the rate voting widget assigned to this entity.'),
          'weight' => 100,
          'visible' => TRUE,
        ];
      }
    }
    if ($comment_module_enabled && $comments && count($comments) > 0) {
      foreach ($comments as $id => $comment) {
        $parameter = explode('.', $comment);
        $fields = $comment_manager
          ->getFields($parameter[0]);
        $field_definitions = $field_manager
          ->getFieldDefinitions($parameter[0], $parameter[1]);
        foreach ($fields as $fid => $field) {
          if (in_array($parameter[1], $field['bundles'])) {
            $comment_settings = $field_definitions[$fid]
              ->getFieldStorageDefinition()
              ->get('settings');
            $comment_type = $comment_settings['comment_type'];
            $widget_name = 'rate_comment_' . $widget;
            $widget_label = 'Rate comment ' . $widget_variables
              ->label();
            $extra['comment'][$comment_type]['display'][$widget_name] = [
              // Create a label from the rate widget name (duplicates are OK).
              'label' => $widget_label,
              'description' => t('Displays the rate voting widget assigned to this entity.'),
              'weight' => 100,
              'visible' => TRUE,
            ];
          }
        }
      }
    }
  }
  return $extra;
}