Page 1 of 1

Custom Index Module

Posted: Fri Aug 16, 2024 10:35 am
by LeeFoster
Morning all,

I am currently working on a custom index model that shows YouTube videos from the shops related channel. I have the code working outside of Phoenix but I'm looking at how to add it in.

I know how to duplicate the module and get that to show, what I'm struggling with is where to put the code for the function that fetches the videos - see below.

Code: Select all

function fetchLatestVideos($apiKey, $channelId, $maxResults = 3) {
    $apiUrl = "https://www.googleapis.com/youtube/v3/search?key=$apiKey&channelId=$channelId&part=snippet,id&order=date&maxResults=$maxResults";
    $response = file_get_contents($apiUrl);
    return json_decode($response, true);
}

$apiKey = ''; // Replace with your YouTube API key
$channelId = ''; // Replace with your YouTube channel ID
$videos = fetchLatestVideos($apiKey, $channelId);

header('Content-Type: application/json');
echo json_encode($videos);

Re: Custom Index Module

Posted: Sun Aug 18, 2024 6:05 am
by heatherbell
LeeFoster wrote: Fri Aug 16, 2024 10:35 am I know how to duplicate the module and get that to show, what I'm struggling with is where to put the code for the function that fetches the videos
Probably misunderstanding but does it not work with that function definition in the main module file and $videos variable then output in the tpl_ file?

Re: Custom Index Module

Posted: Sun Aug 18, 2024 7:55 am
by LeeFoster
heatherbell wrote: Sun Aug 18, 2024 6:05 am
LeeFoster wrote: Fri Aug 16, 2024 10:35 am I know how to duplicate the module and get that to show, what I'm struggling with is where to put the code for the function that fetches the videos
Probably misunderstanding but does it not work with that function definition in the main module file and $videos variable then output in the tpl_ file?
That was my thought too but it didn't work. I've since gotten the module to a point that it displays the boxes for the videos but not the videos themselves and gives the error

Notice: Undefined variable: videoId in includes\modules\pi\index\templates\tpl_i_youtube_videos.php on line 11

Re: Custom Index Module

Posted: Sun Aug 18, 2024 9:15 am
by heatherbell
LeeFoster wrote: Sun Aug 18, 2024 7:55 amUndefined variable: videoId in includes\modules\pi\index\templates\tpl_i_youtube_videos.php on line 11
Don't have an answer. Only guessing the video.id.videoId property might not be defined in the response from the YouTube API. Maybe the video is some type of content that doesn't have a videoId field.

Re: Custom Index Module

Posted: Sun Aug 18, 2024 4:49 pm
by LeeFoster
heatherbell wrote: Sun Aug 18, 2024 9:15 am Don't have an answer. Only guessing the video.id.videoId property might not be defined in the response from the YouTube API. Maybe the video is some type of content that doesn't have a videoId field.
Strange thing is I had this working on it's own before I decided to add it to a Phoenix site.

Re: Custom Index Module

Posted: Mon Aug 19, 2024 2:18 pm
by ecartz
LeeFoster wrote: Fri Aug 16, 2024 10:35 am

Code: Select all

header('Content-Type: application/json');
This is code to make the video the page that you are viewing. There would be different code to embed a video on a particular page with other content.