2 deleted 11 characters in body
source | link

Okey, you wanted a parallel full stack solution, here it is:

You create a new content type:

enter image description here

You should hide the "view count" field from the form display:

enter image description here

Here is the display for teaser:

enter image description here

You need to do some node preprocess in your custom theme (my_custom_themetest.theme):

function test_preprocess_node(&$variables) {
  if ($variables['node']->bundle() == 'my_bounty_content' && $variables['teaser']) {
    _test_preprocess_my_content_teaser($variables);
  }
}

function _test_preprocess_my_content_teaser(&$variables) {
  $variables['content']['field_category'] = $variables['elements']['field_category']['0']['#title'];
  $variables['content']['field_view_count'] = $variables['elements']['field_view_count']['0']['#markup'];
  $comment_count = $variables['node']->get('field_comments')->comment_count;
  $variables['content']['comments_count'] = $comment_count ? $comment_count : 0;
}

You also need the functionality to increase the node view stats by each visit. Put this code into your custom module '.module' file:

/**
 * Implements hook_entity_view().
 */
function base_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'my_bounty_content' && $view_mode == 'full') {
    $view_count = (int) $entity->get('field_view_count')->getString();
    $entity->set('field_view_count', ++$view_count);
    $entity->save();
  }
}

And finally create a new template file for your content teaser in your custom theme directory under 'templates' directory:

node--my-bounty-content--teaser.html.twig

<article>
    <h2>
        {{ label }}
    </h2>
    <ul class="meta">
        <li>{{ author_name }}</li>
        <li>{{ date }}</li>
        <li>{{ content.field_category }}</li>
        <li>Viewed: {{ content.field_view_count }}</li>
        <li>Comments: {{ content.comments_count }}</li>
    </ul>
    <div>{{ content.body }}</div>
    <a href="{{ url }}">{{ 'Read more'|trans  }}</a>

</article>

After this, you only need to list your nodes with teaser view. STILL the views module is the best solution for this, I didn't wrote custom code for this. If you need, let me know I will both post hardcore, and views example for that.

Adding custom theme, library, css is NOT included here.

Remember to clear cache between testing new hooks, templates and so on.

Okey, you wanted a parallel full stack solution, here it is:

You create a new content type:

enter image description here

You should hide the "view count" field from the form display:

enter image description here

Here is the display for teaser:

enter image description here

You need to do some node preprocess in your custom theme (my_custom_theme.theme):

function test_preprocess_node(&$variables) {
  if ($variables['node']->bundle() == 'my_bounty_content' && $variables['teaser']) {
    _test_preprocess_my_content_teaser($variables);
  }
}

function _test_preprocess_my_content_teaser(&$variables) {
  $variables['content']['field_category'] = $variables['elements']['field_category']['0']['#title'];
  $variables['content']['field_view_count'] = $variables['elements']['field_view_count']['0']['#markup'];
  $comment_count = $variables['node']->get('field_comments')->comment_count;
  $variables['content']['comments_count'] = $comment_count ? $comment_count : 0;
}

You also need the functionality to increase the node view stats by each visit. Put this code into your custom module '.module' file:

/**
 * Implements hook_entity_view().
 */
function base_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'my_bounty_content' && $view_mode == 'full') {
    $view_count = (int) $entity->get('field_view_count')->getString();
    $entity->set('field_view_count', ++$view_count);
    $entity->save();
  }
}

And finally create a new template file for your content teaser in your custom theme directory under 'templates' directory:

node--my-bounty-content--teaser.html.twig

<article>
    <h2>
        {{ label }}
    </h2>
    <ul class="meta">
        <li>{{ author_name }}</li>
        <li>{{ date }}</li>
        <li>{{ content.field_category }}</li>
        <li>Viewed: {{ content.field_view_count }}</li>
        <li>Comments: {{ content.comments_count }}</li>
    </ul>
    <div>{{ content.body }}</div>
    <a href="{{ url }}">{{ 'Read more'|trans  }}</a>

</article>

After this, you only need to list your nodes with teaser view. STILL the views module is the best solution for this, I didn't wrote custom code for this. If you need, let me know I will both post hardcore, and views example for that.

Adding custom theme, library, css is NOT included here.

Remember to clear cache between testing new hooks, templates and so on.

Okey, you wanted a parallel full stack solution, here it is:

You create a new content type:

enter image description here

You should hide the "view count" field from the form display:

enter image description here

Here is the display for teaser:

enter image description here

You need to do some node preprocess in your custom theme (test.theme):

function test_preprocess_node(&$variables) {
  if ($variables['node']->bundle() == 'my_bounty_content' && $variables['teaser']) {
    _test_preprocess_my_content_teaser($variables);
  }
}

function _test_preprocess_my_content_teaser(&$variables) {
  $variables['content']['field_category'] = $variables['elements']['field_category']['0']['#title'];
  $variables['content']['field_view_count'] = $variables['elements']['field_view_count']['0']['#markup'];
  $comment_count = $variables['node']->get('field_comments')->comment_count;
  $variables['content']['comments_count'] = $comment_count ? $comment_count : 0;
}

You also need the functionality to increase the node view stats by each visit. Put this code into your custom module '.module' file:

/**
 * Implements hook_entity_view().
 */
function base_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'my_bounty_content' && $view_mode == 'full') {
    $view_count = (int) $entity->get('field_view_count')->getString();
    $entity->set('field_view_count', ++$view_count);
    $entity->save();
  }
}

And finally create a new template file for your content teaser in your custom theme directory under 'templates' directory:

node--my-bounty-content--teaser.html.twig

<article>
    <h2>
        {{ label }}
    </h2>
    <ul class="meta">
        <li>{{ author_name }}</li>
        <li>{{ date }}</li>
        <li>{{ content.field_category }}</li>
        <li>Viewed: {{ content.field_view_count }}</li>
        <li>Comments: {{ content.comments_count }}</li>
    </ul>
    <div>{{ content.body }}</div>
    <a href="{{ url }}">{{ 'Read more'|trans  }}</a>

</article>

After this, you only need to list your nodes with teaser view. STILL the views module is the best solution for this, I didn't wrote custom code for this. If you need, let me know I will both post hardcore, and views example for that.

Adding custom theme, library, css is NOT included here.

Remember to clear cache between testing new hooks, templates and so on.

    Bounty Ended with 50 reputation awarded by Community
1
source | link

Okey, you wanted a parallel full stack solution, here it is:

You create a new content type:

enter image description here

You should hide the "view count" field from the form display:

enter image description here

Here is the display for teaser:

enter image description here

You need to do some node preprocess in your custom theme (my_custom_theme.theme):

function test_preprocess_node(&$variables) {
  if ($variables['node']->bundle() == 'my_bounty_content' && $variables['teaser']) {
    _test_preprocess_my_content_teaser($variables);
  }
}

function _test_preprocess_my_content_teaser(&$variables) {
  $variables['content']['field_category'] = $variables['elements']['field_category']['0']['#title'];
  $variables['content']['field_view_count'] = $variables['elements']['field_view_count']['0']['#markup'];
  $comment_count = $variables['node']->get('field_comments')->comment_count;
  $variables['content']['comments_count'] = $comment_count ? $comment_count : 0;
}

You also need the functionality to increase the node view stats by each visit. Put this code into your custom module '.module' file:

/**
 * Implements hook_entity_view().
 */
function base_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'my_bounty_content' && $view_mode == 'full') {
    $view_count = (int) $entity->get('field_view_count')->getString();
    $entity->set('field_view_count', ++$view_count);
    $entity->save();
  }
}

And finally create a new template file for your content teaser in your custom theme directory under 'templates' directory:

node--my-bounty-content--teaser.html.twig

<article>
    <h2>
        {{ label }}
    </h2>
    <ul class="meta">
        <li>{{ author_name }}</li>
        <li>{{ date }}</li>
        <li>{{ content.field_category }}</li>
        <li>Viewed: {{ content.field_view_count }}</li>
        <li>Comments: {{ content.comments_count }}</li>
    </ul>
    <div>{{ content.body }}</div>
    <a href="{{ url }}">{{ 'Read more'|trans  }}</a>

</article>

After this, you only need to list your nodes with teaser view. STILL the views module is the best solution for this, I didn't wrote custom code for this. If you need, let me know I will both post hardcore, and views example for that.

Adding custom theme, library, css is NOT included here.

Remember to clear cache between testing new hooks, templates and so on.