Custom Index Module

Open to all! Ask other shopowners for help.
Post Reply
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Custom Index Module

Post 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);


Join The Code Co-op to get access to your library in the Code Co-op Forum
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Custom Index Module

Post 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?
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Custom Index Module

Post 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
Last edited by LeeFoster on Sun Aug 18, 2024 8:07 pm, edited 2 times in total.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Custom Index Module

Post 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.
LeeFoster
Contributor
Posts: 263
Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times

Re: Custom Index Module

Post 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.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Custom Index Module

Post 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.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply