You are here

protected function EditorNoteHelperService::generateRow in Editor Notes 8

Generates one Editor Notes table row.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $item: Host entity to attach editor note.

bool $widget: Determines whether to use function in widget along with controls or display it in formatter as just a table without controls.

string $field_name: Field machine name.

int $note_id: Row number.

Return value

array Row data.

1 call to EditorNoteHelperService::generateRow()
EditorNoteHelperService::generateTable in src/EditorNoteHelperService.php
Returns formatted notes table.

File

src/EditorNoteHelperService.php, line 391

Class

EditorNoteHelperService
Class EditorNoteHelperService.

Namespace

Drupal\editor_note

Code

protected function generateRow(ContentEntityInterface $item, $widget, $field_name, $note_id) {

  /** @var \Drupal\user\UserInterface $author */
  $author = $item->uid->entity;
  $author_name = $author
    ->label();
  $row = [
    'data' => [
      'note' => [
        'data' => [
          '#markup' => Xss::filterAdmin($item->note->value),
        ],
        'class' => [
          'note',
        ],
      ],
      'author' => [
        'uid' => $author
          ->id(),
        'data' => $author
          ->hasPermission('administer users') ? $author
          ->toLink($author_name) : $author_name,
        'class' => [
          'author',
        ],
      ],
      'changed' => [
        'data' => $this->dateFormatter
          ->format($item->changed->value, 'short'),
        'class' => [
          'changed',
        ],
      ],
      'created' => [
        'data' => $this->dateFormatter
          ->format($item->created->value, 'short'),
        'class' => [
          'created',
        ],
      ],
    ],
    'class' => [
      Html::cleanCssIdentifier('note-' . $note_id),
    ],
  ];
  $user_access = $this->currentUser
    ->id() == $item->uid->target_id || $this->currentUser
    ->hasPermission('administer any editor note');
  if ($widget && $user_access) {

    // !editor_note_access_crud_operations($field['field_name'], $note_id)
    $edit_link = [
      '#type' => 'link',
      '#title' => $this
        ->t('Edit'),
      '#url' => Url::fromRoute('editor_note.modal_form', [
        'editor_note' => $note_id,
        'nojs' => 'ajax',
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'ctools-modal-' . $field_name . '-edit_link',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode([
          'width' => '50%',
        ]),
      ],
    ];
    $delete_link = [
      '#type' => 'link',
      '#title' => $this
        ->t('Remove'),
      '#url' => Url::fromRoute('editor_note.confirm_delete_editor_note_form', [
        'editor_note' => $note_id,
        'nojs' => 'ajax',
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'ctools-modal-' . $field_name . '-remove',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode([
          'width' => '50%',
        ]),
      ],
    ];
    $basic_items[] = render($edit_link);
    $basic_items[] = render($delete_link);
    $row['data']['operations']['data'] = [
      '#theme' => 'item_list',
      '#items' => $basic_items,
    ];
  }
  return $row;
}