I have a content type which shows video in iframe from youtube. There is a field which I can enter the youtube link and it will render the iframe using the link.
I have created a custom field which will take the youtube link as input and get the youtube statistics using youtube api. The field will display a div in the page with view count and number of likes of the video.
I wanted to access the already existing field value (youtube link which shows iframe), to show my stats information, as both are same. In my custom field module, I am taking an input field which is the youtube url.
For a user, it will be repetitive, if he types the url twice. Also I already have so many contents added to my content types, so it will be a burden to the content creators to go and add values to my newly created field.
Is there any way, I can access the existing fields value of my same content type ?
Here is my code :
YoutubeStat.php
<?php
namespace Drupal\youtube_stat\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface as StorageDefinition;
/**
* Plugin implementation of the 'player_info' field type.
*
* @FieldType(
* id = "Youtube_stat",
* label = @Translation("Youtube statistics"),
* description = @Translation("To Display Youtube statistics"),
* category = @Translation("Custom"),
* default_widget = "Youtube_statDefaultWidget",
* default_formatter = "Youtube_statDefaultFormatter"
* )
*/
class Youtube_stat extends FieldItemBase {
/**
* Field type properties definition.
*
* Inside this method we defines all the fields (properties) that our
* custom field type will have.
*
* Here there is a list of allowed property types: https://goo.gl/sIBBgO
*/
public static function propertyDefinitions(StorageDefinition $storage) {
$properties = [];
$properties['videoId'] = DataDefinition::create('string')
->setLabel(t('Video Id'));
return $properties;
}
/**
* Field type schema definition.
*
* Inside this method we defines the database schema used to store data for
* our field type.
*
* Here there is a list of allowed column types: https://goo.gl/YY3G7s
*/
public static function schema(StorageDefinition $storage) {
$columns = [];
$columns['videoId'] = [
'type' => 'char',
'length' => 255,
];
return [
'columns' => $columns,
'indexes' => [],
];
}
/**
* Define when the field type is empty.
*
* This method is important and used internally by Drupal. Take a moment
* to define when the field fype must be considered empty.
*/
public function isEmpty() {
$isEmpty =
empty($this->get('videoId')->getValue());
return $isEmpty;
}
}
YoutubeStatDefaultFormatter.php
<?php
namespace Drupal\youtube_stat\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal;
/**
* Plugin implementation of the 'Youtube_statDefaultFormatter' formatter.
*
* @FieldFormatter(
* id = "Youtube_statDefaultFormatter",
* label = @Translation("Youtube statisctics"),
* field_types = {
* "Youtube_stat"
* }
* )
*/
class Youtube_statDefaultFormatter extends FormatterBase {
/**
* Define how the field type is showed.
*
* Inside this method we can customize how the field is displayed inside
* pages.
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
\Drupal::service('page_cache_kill_switch')->trigger();
//$memory = (string)ini_get("memory_limit");
//ini_set('memory_limit', '-1');
$elements = [];
foreach ($items as $delta => $item) {
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=*_API_KEY_*&part=statistics&id={$item->videoId}");
$JSON_Data = json_decode($JSON);
$viewCount = $JSON_Data->items[0]->statistics->viewCount;
$likeCount = $JSON_Data->items[0]->statistics->likeCount;
//$dump_data = '<pre>'.json_encode($item).'<pre>';
$stat_div = "<div><span>Views : ".$viewCount."</span> <span>Likes : ".$likeCount."</span></div>";
//$result = $query->fetchAll();
$elements[$delta] = [
'#type' => 'markup',
'#markup' => $stat_div,
'#cache' => ['max-age' => 0]
];
}
//ini_set('memory_limit', $memory);
return $elements;
}
}
Is there any way to achieve this ?